JPA简介
JPA的全称是Java Persistence API,即Java持久化API,是SUN公司推出的一套基于ORM的规范,内部是由一系列的接口和抽象类构成。
通俗的来讲,JPA其实就是一套规范,各个基于ORM的框架比如hibernate在这个规范下实现里面的接口和方法完成各个功能。这样,我们在使用不同的框架时,就不需要修改大量的java代码,只需要把框架的声明修改就可以使用了。类似于JDBC规范和不同的JDBC驱动的关系(mysql驱动和oracle驱动)
JPA入门程序
完成客户的保存功能。
第一步:创建数据库表
CREATE TABLE cst_customer(
cust_id BIGINT(32) PRIMARY KEY NOT NULL AUTO_INCREMENT COMMENT '用户编号(主键)',
cust_name VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
cust_source VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
cust_industry VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
cust_level VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
cust_address VARCHAR(128) DEFAULT NULL COMMENT '客户联系地址',
cust_phone VARCHAR(64) DEFAULT NULL COMMENT '客户联系电话'
);
第二步:创建maven工程,导入jar包
<properties>
<project.hibernate.version>5.0.7.Final</project.hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${project.hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${project.hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
第三步:创建实体类并配置与数据库的映射关系
package com.wuxudong.domain;
import javax.persistence.*;
import java.io.Serializable;
@Entity //声明实体类
@Table(name = "cst_customer") //建立实体类和表的映射关系
public class Customer implements Serializable {
@Id //声明当前私有属性为主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //配置主键的生成策略
@Column(name = "cust_id")
private Long custId;
@Column(name = "cust_name")
private String custName;
@Column(name = "cust_source")
private String custSource;
@Column(name = "cust_industry")
private String custIndustry;
@Column(name = "cust_level")
private String custLevel;
@Column(name = "cust_address")
private String custAddress;
@Column(name = "cust_phone")
private String custPhone;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
}
第四步:配置JPA的核心配置文件
注意:需要在resources目录下创建META-INF文件夹并在该文件夹下创建一个名为persistence.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<!--配置持久化单元
name:持久化单元名称
transaction-type:事务类型
resource_local:本地事务管理 表都存在一个数据库里
jta:分布式事务管理 不同的表存在不同的数据库里
-->
<persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL">
<!--配置JPA规范的服务提供商-->
<provider>
org.hibernate.jpa.HibernatePersistenceProvider
</provider>
<properties>
<!--数据库驱动-->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/wuxudong?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!--jpa提供者的可选配置,我们的JPA规范的提供者为hibernate,所以jpa的核心配置中兼容hibernate配置-->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
第五步:写测试程序
package com.wuxudong.test;
import com.wuxudong.domain.Customer;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JPATest {
@Test
public void test(){
/*
* 创建实体管理类的工厂,借助Persistence静态方法获取
* 其中传递的参数为持久化单元名称,需要jpa配置文件中指定
* */
EntityManagerFactory factory= Persistence.createEntityManagerFactory("myJPA");
//创建实体管理类
EntityManager em=factory.createEntityManager();
//获取事务对象
EntityTransaction tx=em.getTransaction();
//开启事务
tx.begin();
Customer c=new Customer();
c.setCustName("吴许东");
//保存操作
em.persist(c);
//提交事务
tx.commit();
//释放资源
em.close();
factory.close();
}
}