springboot2.0整合jpa

it2024-10-18  15

在整合的遇到各种坑,以下是我整合的流程

1、pom.xml文件

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jooq</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>2、application.properties文件,这里特意说明以下,我这里用的是spring data jpa 2.10版本,支持的数据库驱动有三种,分别是hikari、tomcat-jdbc,dbcp2,优先是hikari,其他两个需要单独导入包: #hikarispring.datasource.hikari.driver-class-name=com.mysql.jdbc.Driverspring.datasource.hikari.jdbc-url=jdbc:mysql://127.0.0.1:3306/yangld?useUnicode=true&characterEncoding=utf-8spring.datasource.hikari.username=rootspring.datasource.hikari.password=1234spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialectspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=create3、JpaConfig类配置有两种形式,一种是xml方式,还有注解的方式,我们用的是springboot,xml的形式是我们要唾弃的,所以用注解的形式: @Configuration@EnableJpaRepositories(basePackages={"com.example.springbootdemo.dao"}) //这个是你Repositorie所在的包@EnableTransactionManagement //这个是事务public class JpaConfig{ @Primary //springboot默认是多数据源,所以你要指定一个主数据源,不然会错误 @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") //需要导入配置 public DataSource dataSource(){ return DataSourceBuilder.create().build(); } @Primary //springboot默认是多数据源,所以你要指定一个主数据源,不然会错误 @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); vendorAdapter.setDatabase(Database.MYSQL);//这里指定的你数据库的类型 LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.example.springbootdemo.entity");//这个是你entity所在的包 factory.setDataSource(dataSource()); return factory; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory); return txManager; }} 4、dao层的Repository @Repositorypublic interface CityRepository extends JpaRepository<City,Integer> {} 5、实体类 @Entity@Getter@Setterpublic class City { @Id @GeneratedValue @Column(name="id") private Integer id; @Column(name="name") private String name; @Column(name="countryCode") private String countryCode; @Column(name="district") private String district; @Column(name="population") private String population;} 6、controller层 @RestControllerpublic class WorkController { @Autowired private CityRepository cityRepository; @RequestMapping("/list") public List<City> queryCityAll(){ return cityRepository.findAll(); }} 7、整合完毕http://localhost:8080/list

转载于:https://www.cnblogs.com/yldIndex/p/9692453.html

最新回复(0)