如何在MySQL数据库和JPA中使用Spring Boot?
我想用MySQL和JPA设置Spring Boot。 为此我创造: 人
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 { Page findAll(Pageable pageable); }
PersonController
package controller; import domain.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import repository.PersonRepository; @Controller public class PersonController { @Autowired private PersonRepository personRepository; @RequestMapping("/") @ResponseBody public String test() { Person person = new Person(); person.setFirstName("First"); person.setLastName("Test"); personRepository.save(person); return "hello"; } }
启动类示例 :
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); } }
对于数据库配置,我创建了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
所以我有项目结构:
但结果我有例外:
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
作为一个例子,我使用: spring-boot-sample-data-jpa / pom.xml
我创建了一个像你一样的项目。 结构看起来像这样
这些课程只是从你的复制粘贴。
我将application.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中 :
4.0.0 org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE spring-boot-sample-jpa Spring Boot JPA Sample Spring Boot JPA Sample mysql mysql-connector-java org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-maven-plugin
检查这些文件是否存在差异 希望这可以帮助
更新1:我更改了用户名。 该示例的链接现在是https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/MySQL
将类移动到特定包(如存储库,控制器,域)时,只需通用@SpringBootApplication
。
您必须指定组件扫描的基础包
@ComponentScan("base_package")
对于JPA
@EnableJpaRepositories(basePackages = "repository")
也需要,因此spring数据将知道存储库接口的位置。
在春季引导参考中 ,它说:
当类不包含包声明时,它被认为是在“默认包”中。 通常不鼓励使用“默认包”,应该避免使用。 对于使用@ComponentScan,@ EntityScan或@SpringBootApplication注释的Spring Boot应用程序,它可能会导致特定问题,因为每个jar中的每个类都将被读取。
com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- web +- CustomerController.java
在你的情况下。 您必须在@SpringBootApplication
注释中添加scanBasePackages
就像@SpringBootApplication(scanBasePackages={"domain","contorller"..})
您可以将Application.java
移动到java下的文件夹中。