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





posted @ 2018-09-23 13:01  来着201780813  阅读(2399)  评论(0编辑  收藏  举报