2019-10-16Jpa的增删改查

package com.shenyue.controller;

import com.shenyue.bean.Customer;
import com.shenyue.utils.JpaUtils;
import org.junit.jupiter.api.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class JpaTest {
/*
    测试jpa的保存
        案例:保存一个客户到数据库中
    jpa的操作步骤
        1.加载配置文件创建工厂(实体管理类工厂)对象
        2.通过实体管理类工厂获取实体管理器
        3.获取事务对象,开启事务
        4.完成增删改查操作
        5.提交事务(回滚事务)
        6.释放资源
* */
@Test
    public void  testSave(){
//        //1.加载配置文件创建工厂(实体管理器工厂)对象
//    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
////        2.通过实体类管理器工厂获取实体管理器
//    EntityManager em = factory.createEntityManager();
    EntityManager em=  JpaUtils.getEntityManager();
//        3.获取事务对象,开启事务
    EntityTransaction tx = em.getTransaction();
    tx.begin();//开启事务
//        4.完成增删改查操作:保存一个客户到数据库中
    Customer customer = new Customer();
    customer.setCustName("SS1111");
    customer.setCustIndustry("教育");
//    保存
    em.persist(customer);//保持操作

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

    /*
     * 根据ID查询用户
     *      getReference方法
     *          1.获取的对象是动态代理对象
     *          2.调用getReference方法不会立即发送sql语句查询数据库
     *              *当调用查询结果对象的时候,才会发送查询的sql语句:什么时候用,什么时候发送sql语句查询数据库
     *延迟加载(懒加载)
     *        *得到的是一个动态代理对象
     *         *什么时候用,什么时候才会查询
     * */
    @Test
    public void testReference(){
        //1.通过工具类获取EntityManager
        EntityManager em = JpaUtils.getEntityManager();
//        2.开启事务
        EntityTransaction tx = em.getTransaction();
        tx.begin();
//        3.增删改查---根据用户ID查询用户
        /*
         * find:根据ID查询数据
         *       class:查询数据的结果需要包装的实体类类型的字节码
         *       id:查询的主键的取值
         * */
        Customer customer = em.getReference(Customer.class, 1l);
        System.out.println(customer);
//        4.提交用户
        tx.commit();
//        5.释放资源
        em.close();
    }
/*
* 删除一个客户的案例
* */
    @Test
    public void testRemove(){
        //1.通过工具类获取EntityManager
        EntityManager em = JpaUtils.getEntityManager();
//        2.开启事务
        EntityTransaction tx = em.getTransaction();
        tx.begin();
//        3.增删改查---根据用户ID查询用户
        /*
        * 根据id查询用户
        * 调用remove方法完成删除操作
        * */
        Customer customer = em.find(Customer.class, 1l);
        em.remove(customer);
//        4.提交用户
        tx.commit();
//        5.释放资源
        em.close();
    }
/*
* 更新用户的操作
*           merge(Object)
* */
    @Test
    public void testUpdata(){
        //1.通过工具类获取EntityManager
        EntityManager em = JpaUtils.getEntityManager();
//        2.开启事务
        EntityTransaction tx = em.getTransaction();
        tx.begin();
//        3.增删改查---根据用户ID查询用户
        /*
         * 根据id查询用户
         * 更新客户
         * */
        Customer customer = em.find(Customer.class, 2l);
        customer.setCustIndustry("银子");
        em.merge(customer);
//        4.提交用户
        tx.commit();
//        5.释放资源
        em.close();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容