Java 如何在MySQL数据库和JPA中使用Spring Boot?
我想用MySQL和JPA设置Spring引导。为此,我创建了: 人Java 如何在MySQL数据库和JPA中使用Spring Boot?,java,mysql,spring,jpa,spring-boot,Java,Mysql,Spring,Jpa,Spring Boot,我想用MySQL和JPA设置Spring引导。为此,我创建了: 人 package domain; import javax.persistence.*; @Entity @Table(name = "person") public class Person { @Id @GeneratedValue private Long id; @Column(nullable = false) private String firstName; // setters and getters }
package domain;
import javax.persistence.*;
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String firstName;
// setters and getters
}
PersonRepository
package repository;
import domain.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, Long> {
Page<Person> findAll(Pageable pageable);
}
开始上课示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Example {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<artifactId>spring-boot-sample-jpa</artifactId>
<name>Spring Boot JPA Sample</name>
<description>Spring Boot JPA Sample</description>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
对于数据库配置,我创建application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.datasource.url=jdbc:mysql://localhost/test_spring_boot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# Data Source properties
spring.datasource.url=jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false
spring.datasource.username=root
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
所以我有一个项目结构:
但结果我有例外:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [Example]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/security/config/annotation/authentication/configurers/GlobalAuthenticationConfigurerAdapter.class] cannot be opened because it does not exist
举个例子:我像你一样创建了一个项目。结构看起来像这样 这些类只是从你的复制粘贴的 我将应用程序.properties更改为:
spring.datasource.url=jdbc:mysql://localhost/testproject
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
但我认为您的问题在于您的pom.xml:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Example {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<artifactId>spring-boot-sample-jpa</artifactId>
<name>Spring Boot JPA Sample</name>
<description>Spring Boot JPA Sample</description>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4.0.0
org.springframework.boot
您可以将应用程序.java
移动到java下的文件夹中。将类移动到存储库、控制器、域等特定包中时,仅使用通用的@SpringBootApplication
是不够的
您必须为组件扫描指定基本包
@ComponentScan("base_package")
JPA
@EnableJpaRepositories(basePackages = "repository")
也需要,因此spring数据将知道在何处查找存储库接口
当一个类不包含包声明时,它被认为是在“默认包”中。通常不鼓励使用“默认包”,应避免使用。对于使用@ComponentScan、@EntityScan或@SpringBootApplication注释的SpringBoot应用程序,它可能会导致特定的问题,因为每个jar中的每个类都将被读取
在你的情况下。您必须在@SpringBootApplication
注释中添加scanBasePackages
。就像@SpringBootApplication(scanBasePackages={“domain”,“contorller.})
对于基于Jpa的应用程序:基本包扫描
@EnableJpaRepositories(basePackages=“存储库”)
你可以试一下
项目结构
应用程序属性
Employee.java
EmployeeRepository.java
您的代码在默认包中,也就是说,您的所有源文件都在src/main/java中,没有自定义包。我强烈建议您创建包n,然后将源文件放入其中
Ex-
src->
main->
java->
com.myfirst.example
Example.java
com.myfirst.example.controller
PersonController.java
com.myfirst.example.repository
PersonRepository.java
com.myfirst.example.model
Person.java
我希望它能解决您的问题。请添加mysql连接器java依赖项
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
那个链接应该告诉我们什么?它是Spring引导数据JPA示例的1.2.2.BUILD-SNAPSHOT的pom.xml。还有,你是如何运行应用程序的?@Steve,我在IDE中运行我的Example.java,它是从命令行运行的吗?不是答案。但是,如果您面临一个问题,您可能会使用不推荐使用的驱动程序类名。改用spring.datasource.drivercassname=com.mysql.cf.jdbc.Driver
。我复制您的更改。很遗憾,我还有一个异常,原因是:java.io.FileNotFoundException:class path resource[org/springframework/security/config/annotation/authentication/configurers/globalaauthenticationConfigureRadapter.class]无法打开,因为它不存在
,然后您的项目中有比您向我们展示的更多的内容。您的问题与spring security有关。你的maven依赖项中还有spring安全性吗?没有。我显示了我所有的架构和类。请稍等。我将把我的项目上传到GitHub,这样你就可以从那里得到它。它应该是完全相同的项目,你的工作。你的代码真的工作!我发现你的计划和我的计划没有什么不同。无论如何,谢谢你!这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论-您可以随时对自己的帖子发表评论,一旦您有足够的评论,您就可以发表评论。-我尝试运行应用程序并得到相同的异常,在将主类移动到文件夹后,应用程序可以运行成功。Mh是和否。如果使用@SpringBootApplication
,则所有组件必须与@SpringBootApplication
注释的类处于相同级别或更低级别。在本例中,这种情况下不需要@ComponentScan
。
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
@RestController
public class EmployeeController {
@Autowired
private EmployeeService empService;
@GetMapping (value = "/employees")
public List<Employee> getAllEmployee(){
return empService.getAllEmployees();
}
@PostMapping (value = "/employee")
public ResponseEntity<Employee> addEmp(@RequestBody Employee emp, HttpServletRequest
request) throws URISyntaxException {
HttpHeaders headers = new HttpHeaders();
headers.setLocation(new URI(request.getRequestURI() + "/" + emp.getId()));
empService.saveEmployee(emp);
return new ResponseEntity<Employee>(emp, headers, HttpStatus.CREATED);
}
public interface EmployeeService {
public List<Employee> getAllEmployees();
public Employee saveEmployee(Employee emp);
}
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository empRepository;
@Override
public List<Employee> getAllEmployees() {
return empRepository.findAll();
}
@Override
public Employee saveEmployee(Employee emp) {
return empRepository.save(emp);
}
}
@SpringBootApplication
@EnableJpaRepositories(basePackages = "repository")
public class EmployeeApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeApplication.class, args);
}
}
Ex-
src->
main->
java->
com.myfirst.example
Example.java
com.myfirst.example.controller
PersonController.java
com.myfirst.example.repository
PersonRepository.java
com.myfirst.example.model
Person.java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
# Data Source properties
spring.datasource.url=jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false
spring.datasource.username=root
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA properties
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update