springboot2.0整合jpa

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot2.0整合jpa相关的知识,希望对你有一定的参考价值。

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

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,其他两个需要单独导入包:
#hikari
spring.datasource.hikari.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.jdbc-url=jdbc:mysql://127.0.0.1:3306/yangld?useUnicode=true&characterEncoding=utf-8
spring.datasource.hikari.username=root
spring.datasource.hikari.password=1234

spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

3、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
@Repository
public interface CityRepository extends JpaRepository<City,Integer> {

}
5、实体类
@Entity
@Getter
@Setter
public 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层
@RestController
public class WorkController {

@Autowired
private CityRepository cityRepository;

@RequestMapping("/list")
public List<City> queryCityAll(){
return cityRepository.findAll();
}
}

7、整合完毕
http://localhost:8080/list




























































































































以上是关于springboot2.0整合jpa的主要内容,如果未能解决你的问题,请参考以下文章

springboot2.0整合springsecurity前后端分离进行自定义权限控制

SpringBoot2.0应用:SpringBoot2.0整合RabbitMQ

SpringBoot2.0源码分析:整合ActiveMQ分析

SpringBoot2.0 整合 SpringSecurity 框架,实现用户权限安全管理

SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+PageHelper