SpringData专题(五)-使用JPA完成CRUD操作

1.抽取JPAUtil工具类

/**
 * @author bruceliu
 * @create 2019-07-06 16:34
 * @description JPA工具类
 */
public final class JPAUtil {
    
    
    // JPA的实体管理器工厂:相当于Hibernate的SessionFactory
    private static EntityManagerFactory em;
    // 使用静态代码块赋值
    static {
        // 注意:该方法参数必须和persistence.xml中persistence-unit标签name属性取值一致
        em = Persistence.createEntityManagerFactory("myJpa");
    }

    /**
     * 使用管理器工厂生产一个管理器对象
     * @return
     */
    public static EntityManager getEntityManager() {
        return em.createEntityManager();
    }
}

2.使用JPA完成增删改查操作

2.1.保存
/**
 * 保存一个实体
 */
@Test
public void testAdd() {
    // 定义对象
    Customer c = new Customer();
    c.setCustName("JAVA学院");
    c.setCustLevel("VIP客户");
    c.setCustSource("网络");
    c.setCustIndustry("IT教育");
    c.setCustAddress("北京XXX学院");
    c.setCustPhone("010-843242X0");
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        // 获取实体管理对象
        em = JPAUtil.getEntityManager();
        // 获取事务对象
        tx = em.getTransaction();
        // 开启事务
        tx.begin();
        // 执行操作
        em.persist(c);
        // 提交事务
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        // 回滚事务
        tx.rollback();
    } finally {
        // 释放资源
        em.close();
    }
}
2.2.修改
@Test
public void testMerge(){
    //定义对象
    EntityManager em=null;
    EntityTransaction tx=null;
    try{
        //获取实体管理对象
        em=JPAUtil.getEntityManager();
        //获取事务对象
        tx=em.getTransaction();
        //开启事务
        tx.begin();
        //执行操作
        Customer c1 = em.find(Customer.class, 1L);
        System.out.println(c1);
        c1.setCustName("JAVA培训学院");
        em.clear();//把c1对象从缓存中清除出去
        em.merge(c1);
        //提交事务
        tx.commit();
    }catch(Exception e){
        //回滚事务
        tx.rollback();
        e.printStackTrace();
    }finally{
        //释放资源
        em.close();
    }
}
2.3.删除
/**
 * 删除
 */
@Test
public void testRemove() {
    // 定义对象
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        // 获取实体管理对象
        em = JPAUtil.getEntityManager();
        // 获取事务对象
        tx = em.getTransaction();
        // 开启事务
        tx.begin();
        // 执行操作
        Customer c1 = em.find(Customer.class, 2L);
        em.remove(c1);
        // 提交事务
        tx.commit();
    } catch (Exception e) {
        // 回滚事务
        tx.rollback();
        e.printStackTrace();
    } finally {
        // 释放资源
        em.close();
    }
}
2.4.根据id查询
    /**
     * 查询一个: 使用立即加载的策略
     */
    @Test
    public void testGetOne() {
        // 定义对象
        EntityManager em = null;
        EntityTransaction tx = null;
        try {
            // 获取实体管理对象
            em = JPAUtil.getEntityManager();
            // 获取事务对象
            tx = em.getTransaction();
            // 开启事务
            tx.begin();
            // 执行操作
            Customer c1 = em.find(Customer.class, 1L);
            // 提交事务
            tx.commit();
            System.out.println(c1); // 输出查询对象
        } catch (Exception e) {
            // 回滚事务
            tx.rollback();
            e.printStackTrace();
        } finally {
            // 释放资源
            em.close();
        }
    }
2.5.查询实体的缓存问题
@Test
public void testGetOne1() {
    // 定义对象
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        // 获取实体管理对象
        em = JPAUtil.getEntityManager();
        // 获取事务对象
        tx = em.getTransaction();
        // 开启事务
        tx.begin();
        // 执行操作
        Customer c1 = em.find(Customer.class, 1L);
        Customer c2 = em.find(Customer.class, 1L);
        System.out.println(c1 == c2);// 输出结果是true,EntityManager也有缓存
        // 提交事务
        tx.commit();
        System.out.println(c1);
    } catch (Exception e) {
        // 回滚事务
        tx.rollback();
        e.printStackTrace();
    } finally {
        // 释放资源
        em.close();
    }
}
2.6.延迟加载策略的方法
@Test
public void testLoadOne() {
	// 定义对象
	EntityManager em = null;
	EntityTransaction tx = null;
	try {
		// 获取实体管理对象
		em = JPAUtil.getEntityManager();
		// 获取事务对象
		tx = em.getTransaction();
		// 开启事务
		tx.begin();
		// 执行操作
		Customer c1 = em.getReference(Customer.class, 1L);
		// 提交事务
		tx.commit();
		System.out.println(c1);
	} catch (Exception e) {
		// 回滚事务
		tx.rollback();
		e.printStackTrace();
	} finally {
		// 释放资源
		em.close();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊猫-IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值