JPA
1、jpa操作的操作步骤
1.加载配置文件创建实体管理器工厂
Persisitence:静态方法(根据持久化单元名称创建实体管理器工厂)
createEntityManagerFactory(持久化单元名称)
作用:创建实体类管理器工厂
2.根据实体管理器工厂,创建实体管理器
EntityManagerFactory:获取EntityManager对象
方法:createEntityManager
* 内部维护的很多的内容
内部维护了数据库信息
维护了缓存信息
维护了所有的实体管理对象
在创建EntityManagerFactory的过程中会根据配置创建数据库表
* EntityManagerFactory的创建过程比较浪费资源
特点:线程安全的对象
多个线程访问同一个EntityManagerFactory不会有线程安全问题
* 如何解决EntityManagerFactory的创建过程浪费资源(耗时)的问题
思路:创建一个公共的EntityManagerFactory的对象
* 静态代码块的形式创建EntityManagerFactory
3.创建事务对象,开启事务
EntityManager对象:实体类管理器
getTransaction:创建事务对象
persist:保存
merge:更新
remove:删除
find/getReference:根据id查询
Transaction对象:事务
begin:开启事务
commit:提交事务
rollback:回滚
4.增删改查操作
5.提交事务
6.释放资源
2、JPA的基本操作
2.1、创建jpa的静态代码块来解决浪费资源问题
/*
* 解决实体管理器工厂的浪费资源和耗时问题
* 通过静态代码块的形式,当程序第一次访问此工具类时,创建一个公共的实体类管理器工厂对象
*
* 第一次访问getEntityManager方法:经过静态代码块创建一个factory对象,再调用方法创建一个EntityManager对象
* 第二次调用方法getEntityManager方法:直接通过一个已经创建好的factory对象,创建EntityManager对象
* */
public class JpaUtils {
private static EntityManagerFactory factory = null;
static{
//1.加载配置文件,创建entityManagerFactory
factory = Persistence.createEntityManagerFactory("myJpa");
}
/*
* 获取EntityManager对象
* */
public static EntityManager getEntityManager(){
EntityManager entityManager = factory.createEntityManager();
return entityManager;
}
}
2.2、改造测试方法
@Test
public void testSave(){
EntityManager em = JpaUtils.getEntityManager();
//3.获取事务对象,开启事务
EntityTransaction tx = em.getTransaction();//获取事务对象
tx.begin(); //开启事务
//4.完成增删改查操作:保存一个客户到数据库中
Customer c = new Customer();
c.setCustName("实达迪美");
em.persist(c);//保存操作
//5.提交事务
tx.commit();
//6.释放资源
em.close();
}
2.3、根据id查询客户
find方法:
/*
* 根据id查询客户
* 使用find方法查询:
* 1.查询的对象就是当前客户对象本身
* 2.在调用find方法的时候,就会发送sql语句查询数据库
* 立即加载
* */
@Test
public void testFind(){
//1.通过工具类获取实体类管理器对象
EntityManager em = JpaUtils.getEntityManager();
//2.获取事务管理器对象
EntityTransaction tx = em.getTransaction();
//3.开启事务
tx.begin();
//4.根据id查询对象
/*
* find:根据id查询数据
* class:查询数据的结果需要包装的实体类类型的字节码
* id:查询的主键的取值
* */
Customer customer = em.find(Customer.class, 1l);
System.out.println(customer);
//5.提交事务
tx.commit();
//6.关闭资源
em.close();
}
getReference方法:
/*
* 根据id查询客户
* 使用getReference方法查询:
* 1.获取的对象是一个动态代理对象
* 2.调用getReference方法不会立即发送sql语句查询数据库
* * 当调用查询结果对象的时候,才会发送查询的sql语句:什么时候用,什么时候发送sql语句查询数据库
*
*延迟加载(懒加载)
* * 得到的是一个动态代理对象
* * 什么时候用,才会查询
* */
@Test
public void testGetReference(){
//1.通过工具类获取实体类管理器对象
EntityManager em = JpaUtils.getEntityManager();
//2.获取事务管理器对象
EntityTransaction tx = em.getTransaction();
//3.开启事务
tx.begin();
//4.根据id查询对象
/*
* find:根据id查询数据
* class:查询数据的结果需要包装的实体类类型的字节码
* id:查询的主键的取值
* */
Customer customer = em.getReference(Customer.class, 1l);
System.out.println(customer);
//5.提交事务
tx.commit();
//6.关闭资源
em.close();
}
2.4、删除客户
实体类管理器对象的remove传递的是一个Object对象,所以要删除一个对象得先根据id查询出来
/*
*删除客户的案例
* */
@Test
public void testRemove(){
//1.通过工具类获取实体类管理器对象
EntityManager em = JpaUtils.getEntityManager();
//2.获取事务管理器对象
EntityTransaction tx = em.getTransaction();
//3.开启事务
tx.begin();
//4.删除客户
//根据id查询用户
Customer customer = em.find(Customer.class, 1l);
em.remove(customer);
//5.提交事务
tx.commit();
//6.关闭资源
em.close();
}
2.5、更新客户
/*
*更新客户的操作
* */
@Test
public void testUpdate(){
//1.通过工具类获取实体类管理器对象
EntityManager em = JpaUtils.getEntityManager();
//2.获取事务管理器对象
EntityTransaction tx = em.getTransaction();
//3.开启事务
tx.begin();
//4.删除客户
// i 根据id查询用户
Customer customer = em.find(Customer.class, 2l);
customer.setCustLevel("5");
customer.setCustPhone("123456");
// ii 根据id查询用户java
em.merge(customer);
//5.提交事务
tx.commit();
//6.关闭资源
em.close();
}