什么是JPA?
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
The Java Persistence API provides a POJO persistence model for object-relational mapping. The Java Persistence API was developed by the EJB 3.0 software expert group as part of JSR 220, but its use is not limited to EJB software components. It can also be used directly by web applications and application clients, and even outside the Java EE platform, for example, in Java SE applications. See JSR 220.
从另一方面来说,JPA 是 JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的架构,提供相同的访问API,这保证了基于JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行。
JPA是需要Provider来实现其功能的,Hibernate就是JPA Provider中很强的一个,应该说无人能出其右。从功能上来说,JPA就是Hibernate功能的一个子集。Hibernate 从3.2开始,就开始兼容JPA。Hibernate3.2获得了Sun TCK的JPA(Java Persistence API) 兼容认证。
更清晰的概念可以看一下JPA(一):十分钟入门 JPA这篇博客中的思维导图。
Sqlite DB 简介
SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成。
项目添加maven依赖
首先修改pom.xml,这里说一下,这个示例项目是用idea创建的maven poject。关键配置如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netty.version>4.0.21.Final</netty.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</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>
<!--sqlite-->
<dependency>
<groupId>com.enigmabridge</groupId>
<artifactId>hibernate4-sqlite-dialect</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
使用@Configuration配置数据源
数据源配置使用spring的Configuration注解配置,在 conf目录(或其他spring能遍历到的目录下)创建DataSourceConfig.java,编写如下内容:
/**
* 配置sqlite db数据源
* @author : vicsun
* @version V1.0
*/
@Configuration
public class DataSourceConfig {
@Bean(destroyMethod = "", name = "EmbeddedDataSource")
public DataSource dataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.sqlite.JDBC");
// sqlite文件路径,可以是绝对路径也可以是相对路径
// dataSourceBuilder.url("jdbc:sqlite:C:/...省略 .../main/resources/static/db\\normal.db");
dataSourceBuilder.url("jdbc:sqlite:src/main/resources/static/db/normal.db");
dataSourceBuilder.type(SQLiteDataSource.class);
return dataSourceBuilder.build();
}
}
这里如果sqlite的文件路径或依赖有问题会出现如下错误:
Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
JPA配置
同样使用@Configuration注解。
/**
* sqlite db jpa 配置
* @author : vicsun
* @version V1.0
*/
@Configuration
@EnableJpaRepositories(
basePackages = "com.epic.demo",
transactionManagerRef = "jpaTransactionManager",
entityManagerFactoryRef = "localContainerEntityManagerFactoryBean"
)
@EnableTransactionManagement
public class JpaConfig {
@Autowired
@Bean
public JpaTransactionManager jpaTransactionManager(@Qualifier(value = "EmbeddedDataSource") DataSource dataSource, EntityManagerFactory entityManagerFactory) {
JpaTransactionManager jpaTransactionManager
= new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
jpaTransactionManager.setDataSource(dataSource);
return jpaTransactionManager;
}
@Autowired
@Bean
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(@Qualifier(value = "EmbeddedDataSource") DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean
= new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource);
// 遍历模型,自动创建表
localContainerEntityManagerFactoryBean.setPackagesToScan("com.epic.demo.model");
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
return localContainerEntityManagerFactoryBean;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setDatabasePlatform("com.enigmabridge.hibernate.dialect.SQLiteDialect");
return hibernateJpaVendorAdapter;
}
}
总结
这样我们就完成了基本的数据源配置,可以进行下一步开发。