JPA
1、依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.hibernate.version>5.0.7.Final</project.hibernate.version>
</properties>
<dependencies>
<!--hibernate对jpa的支持-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${project.hibernate.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${project.hibernate.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
2、配置文件
JPA的配置文件必须配置在资源目录的META-INF目录下,文件名persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<!--
jpa持久化单元结点:
name:持久化单元名
transaction-type:事务管理策略
"RESOURCE_LOCAL":本地事务管理
"JAT":分布式事务管理
-->
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<!--jpa实现方式-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--数据库信息-->
<properties>
<!--数据库配置-->
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.44.130:3306/testJpa?characterEncoding=utf8"/>
<!--hibernate配置-->
<!--是否打印语句-->
<property name="hibernate.show_sql" value="true"/>
<!--这个参数有create、create-drop、update、none四个可选值-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
<!--可选配置:jpa实现方的配置-->
</persistence-unit>
</persistence>
3、定义的实体类UserEntity
package com.xsl.entity;
import javax.persistence.*;
/**
*Entity:声明这个类是实体类
*Table:实体类和表的映射关系
* name:配置数据库表的名称
*/
@Entity
@Table(name = "user")
public class UserEntity {
/**
* Id:指定主键
* GeneratedValue:主键生成策略这里采用的是自增(IDENTITY)还有SEQUENCE、AUTO、TABLE
* 必须支持才能使用
* Column:属性和字段的映射
*/
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
public UserEntity(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserEntity() {
}
}
4、JpaEntityManagerFactory工具类
用户在需要使用到JPA时使用这个工厂类加载配置文件创建实体类管理工厂,并且给客户提供EntityManager
package com.xsl.utils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JpaEntityManagerFactory {
private static EntityManagerFactory emf = null;
static {
emf = Persistence.createEntityManagerFactory("jpa");
}
public static EntityManager getEntityManager(){
return emf.createEntityManager();
}
}
5、JpaTest测试类测试JPA
package com.xsl.test;
import com.xsl.entity.UserEntity;
import com.xsl.utils.JpaEntityManagerFactory;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* 测试jpa
* jpa操作步骤:
* 1、加载配置文件创建工厂(实体管理类工厂)对象
* 2、通过实体管理类工厂获取实体管理器
* 3、获取事务对象,开启事务
* 4、持久化
* 5、提交/回滚事务
* 6、释放资源
*/
public class JpaTest {
@Test
public void testSave(){
EntityManager em = JpaEntityManagerFactory.getEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
//添加
em.persist(new UserEntity("xsl","123"));
//查找
// UserEntity userEntity = em.getReference(UserEntity.class, 1);
UserEntity userEntity = em.find(UserEntity.class, 1);
//修改
em.merge(userEntity);
//删除
em.remove(userEntity);
System.out.println(userEntity);
tx.commit();
em.close();
}
}
6、JpqlTest测试类测试jpql
package com.xsl.test;
import com.xsl.entity.UserEntity;
import com.xsl.utils.JpaEntityManagerFactory;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;
public class JpqlTest {
/*
*查询所有
*/
@Test
public void testFindAll(){
//获取EntityManager
EntityManager entityManager = JpaEntityManagerFactory.getEntityManager();
//开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查询
String jpql = "from UserEntity order by id DESC";
Query query = entityManager.createQuery(jpql);
List resultList = query.getResultList();
for (Object user:resultList) {
System.out.println(user);
}
//提交
transaction.commit();
//关闭
entityManager.close();
}
/*
*计数
*/
@Test
public void testCount(){
//获取EntityManager
EntityManager entityManager = JpaEntityManagerFactory.getEntityManager();
//开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查询
String jpql = "select count(id) from UserEntity";
Query query = entityManager.createQuery(jpql);
Object singleResult = query.getSingleResult();
System.out.println(singleResult);
//提交
transaction.commit();
//关闭
entityManager.close();
}
/*
*分页
*/
@Test
public void testPage(){
//获取EntityManager
EntityManager entityManager = JpaEntityManagerFactory.getEntityManager();
//开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查询
String jpql = "from UserEntity";
Query query = entityManager.createQuery(jpql);
//第一个数据
query.setFirstResult(0);
//最多多少个数据
query.setMaxResults(1);
//提交
List resultList = query.getResultList();
System.out.println(resultList);
transaction.commit();
//关闭
entityManager.close();
}
/*
*赋值
*/
@Test
public void testLike(){
//获取EntityManager
EntityManager entityManager = JpaEntityManagerFactory.getEntityManager();
//开启事务
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//查询
String jpql = "from UserEntity where username like ?";
Query query = entityManager.createQuery(jpql);
query.setParameter(1,"%xsl%");
//提交
List resultList = query.getResultList();
System.out.println(resultList);
transaction.commit();
//关闭
entityManager.close();
}
}