什么是ORM:
即Object-Relationl Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样,我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作对象一样操作它就可以了 。
jpa最关键的操作就是建立数据库与实体之间的联系,然后就可以通过这个实体来操作数据库。
下面用JPA简单的实现了CURD操作:
1.创建(Create)
@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("传智播客");
customer.setCustIndustry("教育");
//保存,
em.persist(customer); //保存操作
//5.提交事务
tx.commit();
//6.释放资源
em.close();
// factory.close();
}
2.更新(Update)
/**
* 更新客户的操作
* merge(Object)
*/
@Test
public void testUpdate() {
//1.通过工具类获取entityManager
EntityManager entityManager = JpaUtils.getEntityManager();
//2.开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.增删改查 -- 更新操作
//i 查询客户
Customer customer = entityManager.find(Customer.class,1l);
//ii 更新客户
customer.setCustIndustry("it教育");
entityManager.merge(customer);
//4.提交事务
tx.commit();
//5.释放资源
entityManager.close();
}
3.读取(Retrieve)
/**
* 根据id查询客户
* 使用find方法查询:
* 1.查询的对象就是当前客户对象本身
* 2.在调用find方法的时候,就会发送sql语句查询数据库
*
* 立即加载
*
*
*/
@Test
public void testFind() {
//1.通过工具类获取entityManager
EntityManager entityManager = JpaUtils.getEntityManager();
//2.开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.增删改查 -- 根据id查询客户
/**
* find : 根据id查询数据
* class:查询数据的结果需要包装的实体类类型的字节码
* id:查询的主键的取值
*/
Customer customer = entityManager.find(Customer.class, 1l);
// System.out.print(customer);
//4.提交事务
tx.commit();
//5.释放资源
entityManager.close();
}
/**
* 根据id查询客户
* getReference方法
* 1.获取的对象是一个动态代理对象
* 2.调用getReference方法不会立即发送sql语句查询数据库
* * 当调用查询结果对象的时候,才会发送查询的sql语句:什么时候用,什么时候发送sql语句查询数据库
*
* 延迟加载(懒加载)
* * 得到的是一个动态代理对象
* * 什么时候用,什么使用才会查询
*/
@Test
public void testReference() {
//1.通过工具类获取entityManager
EntityManager entityManager = JpaUtils.getEntityManager();
//2.开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.增删改查 -- 根据id查询客户
/**
* getReference : 根据id查询数据
* class:查询数据的结果需要包装的实体类类型的字节码
* id:查询的主键的取值
*/
Customer customer = entityManager.getReference(Customer.class, 1l);
System.out.print(customer);
//4.提交事务
tx.commit();
//5.释放资源
entityManager.close();
}
4.删除(Delete)
/**
* 删除客户的案例
*
*/
@Test
public void testRemove() {
//1.通过工具类获取entityManager
EntityManager entityManager = JpaUtils.getEntityManager();
//2.开启事务
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
//3.增删改查 -- 删除客户
//i 根据id查询客户
Customer customer = entityManager.find(Customer.class,1l);
//ii 调用remove方法完成删除操作
entityManager.remove(customer);
//4.提交事务
tx.commit();
//5.释放资源
entityManager.close();
}
最后附上一个工具类
/**
* 解决实体管理器工厂的浪费资源和耗时问题
* 通过静态代码块的形式,当程序第一次访问此工具类时,创建一个公共的实体管理器工厂对象
*
* 第一次访问getEntityManager方法:经过静态代码块创建一个factory对象,再调用方法创建一个EntityManager对象
* 第二次方法getEntityManager方法:直接通过一个已经创建好的factory对象,创建EntityManager对象
*/
public class JpaUtils {
private static EntityManagerFactory factory;
static {
//1.加载配置文件,创建entityManagerFactory
factory = Persistence.createEntityManagerFactory("myJpa");
}
/**
* 获取EntityManager对象
*/
public static EntityManager getEntityManager() {
return factory.createEntityManager();
}
}