JPA 概念
JPA(Java Persistence API)用於對象持久化的 API,是 Java EE 5.0 平台標准的 ORM 規范,使得應用程序以統一的方式訪問持久層。
與 JDBC 的對比
JDBC 也是一種規范和接口,不過 JDBC 是面向 SQL 的,使用起來比較繁瑣。所以就有了 ORM 框架,建立了 Java 對象與數據庫表之間的映射關系,可以通過直接操作對象來實現持久化,簡化了操作的繁雜度。而 JPA 就是 ORM 框架的規范,值得一提的是 Hibernate 是符合 JPA 規范的,而 MyBatis 卻不符合,因為 MyBatis 還是需要寫 SQL 的。
JDBC 示意圖:

JPA 示意圖:

例子
在 IDEA 下創建一個 JPA 項目,並實現基本的 CRUD。
1、創建一個 JavaEE Persistence 項目,具體如下所示

采用 Hibernate 實現 JPA。
2、導入相應的 Maven 依賴

添加 Maven 支持。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>jpa-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</project>
3、配置數據庫(MySQL 8)相關內容 persistence.xml
注意此文件要位於類路徑下,這里放在 resources/META-INF 下。
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="jpa-1">
<!--是 PersistenceProvider 接口的實現類-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--添加持久化類-->
<class>com.yunche.helloworld.Customer</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa?useSSL=false&serverTimezone=Asia/Shanghai"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!--注意這個屬性,自動生成的文件前面沒有 hibernate,要加上 hibernate -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- 使用 MySQL8Dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
4、新建一個持久化對象類
package com.yunche.helloworld;
import javax.persistence.*;
/**
* @ClassName: Customer
* @Description:
* @author: yunche
* @date: 2019/01/16
*/
@Entity(name = "customers")
public class Customer {
private Integer age;
private String lastName;
private Integer id;
private String email;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "last_name")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
並在 persistence.xml 中的 persistence-unit 節點下加入:
<!--添加持久化類-->
<!--后來發現:似乎可以不加-->
<class>com.yunche.helloworld.Customer</class>
5、Main 類
package com.yunche.helloworld;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* @ClassName: Main
* @Description:
* @author: yunche
* @date: 2019/01/16
*/
public class Main {
public static void main(String[] args) {
//persistence.xml 中的 persistence-unit
String persistenceUnitName = "jpa-1";
//1、創建 EntityManagerFactory
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
//2、創建 EntityManager
EntityManager entityManager = entityManagerFactory.createEntityManager();
//3、開啟事務
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//4、進行持久化操作
Customer customer = new Customer();
customer.setAge(13);
customer.setLastName("li");
customer.setEmail("123@qq.com");
entityManager.persist(customer);
//5、提交事務
transaction.commit();
//6、關閉 EntityManager
entityManager.close();
//7、關閉 EntityManagerFactory
entityManagerFactory.close();
}
}
6、結果


7、注意事項
我后來發現了一點問題,關於包 javax.persistence-api 的,我創建項目的時候選中了 persistence 2.0 , IDEA 自動導入了 javax.persistence-api-2.0.jar,而 maven 下的 hibernate-entitymanager 依賴含有 javax.persistence-api-2.2.jar ,所以 jar 包重復了,並且我發現2.0版本的有些方法沒有,所以還是將2.0版本的jar包刪除,用 2.2 版本的。
參考資料
尚硅谷 佟剛 JPA。