使用 JPA 完成增删改查操作

保存

    public void testSave(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.获取事务对象,开启事务
        EntityTransaction tx = entityManager.getTransaction();//获取事务对象
        tx.begin();//开启事务
        //3.完成增删改查操作
        Customer customer = new Customer();
        customer.setCustName("LEEWLE");
        customer.setCustIndustry("体育");
        entityManager.persist(customer);//保存操作
        //4.提交事务(回滚事务)
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

删除

    //删除一个客户(根据id)
    @Test
    public void testRemove(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
        //① 根据id查询客户   ② 调用remove方法完成删除操作
        Customer customer = entityManager.find(Customer.class, 1l);
        entityManager.remove(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

更新


    //更新客户的操作(merge)
    @Test
    public void testUpdate(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
        //① 根据 id 查询客户
        Customer customer = entityManager.find(Customer.class, 2l);
        //2.调用merge方法完成更新操作
        customer.setCustIndustry("美术");
        entityManager.merge(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

查询

	
	//使用find方法根据 id 查询客户(立即加载):
	//1.查询对象就是当前客户对象本身
	//2.在调用find方法的时候,就会发送sql语句查询数据库
    @Test
    public void testFind(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
		//参数:class(查询数据的结果需要包装的实体类型的字节码)id:查询的主键的取值
        Customer customer = entityManager.find(Customer.class, 1l);
        System.out.println(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }

   
	//使用getReference方法根据 id 查询客户(延迟加载):
    //1.获取的对象是一个动态代
    //2.在调用getReference方法的时候,不会立即发送sql语句查询数据库
    //(什么时候用,什么时候发送sql查询数据库)
    @Test
    public void testReference(){
        //1.使用工具类获取EntityManager对象(实体管理器)
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2.开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        //3.执行增删改查
		//参数:class(查询数据的结果需要包装的实体类型的字节码) id:查询的主键的取值
        Customer customer = entityManager.getReference(Customer.class, 1l);
        System.out.println(customer);
        //4.提交事务
        tx.commit();
        //5.释放资源
        entityManager.close();
    }
发布了165 篇原创文章 · 获赞 8 · 访问量 8989

猜你喜欢

转载自blog.csdn.net/wait_13/article/details/104331238