JPA的基本操作
1 api对象
jpa操作的操作步骤
1.加载配置文件创建实体管理器工厂
Persisitence:静态方法(根据持久化单元名称创建实体管理器工厂)。
createEntityMnagerFactory(持久化单元名称)作用:创建实体管理器工厂。
2.根据实体管理器工厂,创建实体管理器
EntityManagerFactory :获取EntityManager对象
方法:createEntityManager
内部维护的很多的内容:
内部维护了数据库信息。
维护了缓存信息。
维护了所有的实体管理器对象。
再创建EntityManagerFactory的过程中会根据配置创建数据库表。
EntityManagerFactory的创建过程比较浪费资源。
特点:线程安全的对象。
多个线程访问同一个EntityManagerFactory不会有线程安全问题。
如何解决EntityManagerFactory的创建过程浪费资源(耗时)的问题?
思路:创建一个公共的EntityManagerFactory的对象。
静态代码块的形式创建EntityManagerFactory。
3.创建事务对象,开启事务
EntityManager对象:实体类管理器
beginTransaction : 创建事务对象
presist : 保存
merge : 更新
remove : 删除
find/getRefrence : 根据id查询
Transaction 对象 : 事务
begin:开启事务
commit:提交事务
rollback:回滚
4.增删改查操作
5.提交事务
6.释放资源
2 抽取jpaUtils工具类
编写jpaUtils工具类的代码如下:
package com.txw.utils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
解决实体管理器工厂的浪费资源和耗时问题
通过静态代码块的形式,当程序第一次访问此工具类时,创建一个公共的实体管理器工厂对象
第一次访问getEntityManager方法:经过静态代码块创建一个factory对象,再调用方法创建一个EntityManager对象
第二次方法getEntityManager方法:直接通过一个已经创建好的factory对象,创建EntityManager对象
* @author: Adair
*/
@SuppressWarnings("all") // 注解警告信息
public class JpaUtils {
// 声明EntityManagerFactory业务对象
private static EntityManagerFactory factory;
static {
// 1.加载配置文件,创建entityManagerFactory对象
factory = Persistence.createEntityManagerFactory("myJpa");
}
/**
* 获取EntityManager对象
*/
public static EntityManager getEntityManager() {
return factory.createEntityManager();
}
}
3 验证工具类
修改测试类的代码如下:
package com.txw.test;
import com.txw.domain.Customer;
import com.txw.utils.JpaUtils;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* 测试jpa
* @author: Adair
*/
@SuppressWarnings("all") // 注解警告信息
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 = (EntityManager) JpaUtils.getEntityManager();
// 3.获取事务对象,开启事务
EntityTransaction tx = em.getTransaction(); // 获取事务对象
tx.begin(); // 开启事务
// 4.完成增删改查操作:保存一个客户到数据库中
Customer customer = new Customer();
customer.setCustName("学无止路");
customer.setCustSource("微信公众号");
customer.setCustIndustry("笔记");
customer.setCustLevel("1及");
customer.setCustAddress("贵州省六盘水市");
customer.setCustPhone("15788533828*");
//保存,
em.persist(customer); //保存操作
// 5.提交事务
tx.commit();
// 6.释放资源
em.close();
// factory.close();
}
}
运行结果如图所示:
使用 SELECT * FROM cst_customer;在数据库中运行结果如图所示:
4 根据id查询客户
编写根据id查询客户的代码如下:
import com.txw.domain.Customer;
import com.txw.utils.JpaUtils;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
/**
* 测试jpa
* @author: Adair
*/
@SuppressWarnings("all") // 注解警告信息
public class JpaTest {
/**
* 根据id查询客户
* 使用find方法查询:
* 1.查询的对象就是当前客户对象本身
* 2.在调用find方法的时候,就会发送sql语句查询数据库
* 立即加载
*/
@Test
public void testFind() {
// 1.通过工具类获取entityManager
EntityManager em = (EntityManager) JpaUtils.getEntityManager();
// 获取事务对象
EntityTransaction tx = em.getTransaction();
// 2.开启事务
tx.begin();
// 3.增删改查 -- 根据id查询客户
/**
* find : 根据id查询数据
* class:查询数据的结果需要包装的实体类类型的字节码
* id:查询的主键的取值
*/
Customer customer = em.find(Customer.class, 6l);
System.out.print(customer);
// 4.提交事务
tx.commit();
// 5.释放资源
em.close();
}
}
运行结果如图所示:
5 延迟加载与立即加载
编写延迟加载的测试代码如下:
package com.txw.test;
import com.txw.domain.Customer;
import com.txw.utils.JpaUtils;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
/**
* 测试jpa
* @author: Adair
*/
@SuppressWarnings("all") // 注解警告信息
public class JpaTest {
/**
* 根据id查询客户
* getReference方法
* 1.获取的对象是一个动态代理对象
* 2.调用getReference方法不会立即发送sql语句查询数据库.
* 当调用查询结果对象的时候,才会发送查询的sql语句:什么时候用,什么时候发送sql语句查询数据库
* 延迟加载(懒加载)
* * 得到的是一个动态代理对象
* * 什么时候用,什么使用才会查询
*/
@Test
public void testReference() {
// 1.通过工具类获取entityManager
EntityManager em = (EntityManager) JpaUtils.getEntityManager();
// 获取事务对象
EntityTransaction tx = em.getTransaction();
// 2.开启事务
tx.begin();
// 3.增删改查 -- 根据id查询客户
/**
* getReference : 根据id查询数据
* class:查询数据的结果需要包装的实体类类型的字节码
* id:查询的主键的取值
*/
Customer customer = em.getReference(Customer.class, 6l);
System.out.print(customer);
// 4.提交事务
tx.commit();
// 5.释放资源
em.close();
}
}
运行简单如图所示:
6 据id删除客户
测试代码如下:
package com.txw.test;
import com.txw.domain.Customer;
import com.txw.utils.JpaUtils;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
/**
* 测试jpa
* @author: Adair
*/
@SuppressWarnings("all") // 注解警告信息
public class JpaTest {
/**
* 删除客户的案例
*/
@Test
public void testRemove() {
// 1.通过工具类获取entityManager
EntityManager em = (EntityManager) JpaUtils.getEntityManager();
// 获取事务对象
EntityTransaction tx = em.getTransaction();
// 2.开启事务
tx.begin();
// 3.增删改查 -- 删除客户
Customer customer = em.getReference(Customer.class, 3l);
// 调用remove方法完成删除操作
em.remove(customer);
// 4.提交事务
tx.commit();
// 5.释放资源
em.close();
}
}
运行之前数据库如图所示:
运行测试结果如图所示:
运行之后数据库如图所示:
7 客户的更新操作
测试代码如下:
package com.txw.test;
import com.txw.domain.Customer;
import com.txw.utils.JpaUtils;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
/**
* 测试jpa
* @author: Adair
*/
@SuppressWarnings("all") // 注解警告信息
public class JpaTest {
/**
* 更新客户的操作
* merge(Object)
*/
@Test
public void testUpdate() {
// 1.通过工具类获取entityManager
EntityManager em = (EntityManager) JpaUtils.getEntityManager();
// 获取事务对象
EntityTransaction tx = em.getTransaction();
// 2.开启事务
tx.begin();
// 3.增删改查 -- 更新客户
Customer customer = em.getReference(Customer.class, 4l);
customer.setCustLevel("1级");
// 更新客户
em.merge(customer);
// 4.提交事务
tx.commit();
// 5.释放资源
em.close();
}
}
运行之前数据库如图所示:
运行测试结果如图所示:
运行之后数据库如图所示: