一.查询
1.find方法【立即加载】
(1)代码块
/**
* 测试根据id查询客户
* 方式一
* find方法在执行时,则获取的结果是实体类对象。立即加载
*/
@Test
public void test3(){
//1.通过工具类获取对象
EntityManager entityManager = JPAUtil.getEntityManager();
//2.获取事务对象,并开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.执行sql语句【查询操作】
//参数一:实体类的字节码,参数二:id主键的属性值
Customer customer = entityManager.find(Customer.class, 1l);
// System.out.println(customer);
//4.事务提交
tx.commit();
//5.关闭资源
entityManager.close();
// factory.close(); //工厂需要共享,不需要关闭
}
(2)范例
2.getTransaction【延迟加载】
(1)代码块
/**
* 测试根据id查询客户
* 方式二
* getReference方法获取的是代理对象,当调用实体类对象时,则发送sql语法进行查询
*/
@Test
public void test4(){
//1.通过工具类获取对象
EntityManager entityManager = JPAUtil.getEntityManager();
//2.获取事务对象,并开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.执行sql语句【查询操作】
//参数一:实体类的字节码,参数二:id主键的属性值
Customer customer = entityManager.getReference(Customer.class, 1l);
// System.out.println(customer);
//4.事务提交
tx.commit();
//5.关闭资源
entityManager.close();
// factory.close(); //工厂需要共享,不需要关闭
}
(2)范例
二.删除
1.代码块
/**
* 测试根据id删除客户
* getReference方法根据id查询用户
* remove方法根据id进行删除
*/
@Test
public void test5(){
//1.通过工具类获取对象
EntityManager entityManager = JPAUtil.getEntityManager();
//2.获取事务对象,并开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.执行sql语句【查询操作】
//3.1根据id查询用户
Customer customer = entityManager.getReference(Customer.class, 1l);
//3.2使用用户对象删除记录
//参数:id主键的属性值
entityManager.remove(customer);//自动查找实体类对象中的主键属性
//4.事务提交
tx.commit();
//5.关闭资源
entityManager.close();
// factory.close(); //工厂需要共享,不需要关闭
}
2.范例
三.更新
1.代码块
/**
* 测试根据id更新客户
* remove方法根据id进行查询
*/
@Test
public void test6(){
//1.通过工具类获取对象
EntityManager entityManager = JPAUtil.getEntityManager();
//2.获取事务对象,并开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.执行sql语句【查询操作】
//3.1根据id查询用户
Customer customer = entityManager.getReference(Customer.class, 1l);
customer.setCustName("小李");
//3.2使用用户对象更新记录
//参数:实体类对象
entityManager.merge(customer);//自动查找实体类对象中的主键属性
//4.事务提交
tx.commit();
//5.关闭资源
entityManager.close();
// factory.close(); //工厂需要共享,不需要关闭
}
2.范例
四.添加操作
1.代码块
/**
* 测试jpa的保存
*/
/*
jpa的操作步骤
1.加载配置文件创建工厂(实体管理工厂)对象
作用:创建工厂
2.通过工厂获取实体管理器
作用:获取EntityManager对象
EntityManager对象内部维护了很多内容。
(1)维护了数据库信息
(2)维护缓存信息
(3)维护了所以实体类对象
(4)在创建EntityManager对象通过配置文件创建数据库表中映射的实体类对象
总结:创建EntityManager对象比较浪费资源.
特点:线程安全。
3.开启事务
4.执行sql语句【增删改查】
EntityManager对象的方法
persist:保存方法
merge:修改方法
remove:删除方法
find/getReference:查询方法【根据id】
5.提交事务
6.关闭资源
*/
@Test
public void test1(){
//1.加载配置文件创建工厂(实体管理工厂)对象
//参数是持久化单元名称
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
//2.通过工厂获取实体管理器
EntityManager entityManager = factory.createEntityManager();
//3.获取事务对象,并开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//4.执行sql语句【保存操作】
//4.1创建对象
Customer customer = new Customer();
customer.setCustName("aaa");
customer.setCustIndustry("辽宁大连");
//4.2保存操作
entityManager.persist(customer);
//5.事务提交
tx.commit();
//6.关闭资源
entityManager.close();
factory.close();
}
2.范例
五.源码
day01.rar