快速學習-使用JPA完成增刪改查操作
- 2020 年 4 月 1 日
- 筆記
第7章 使用JPA完成增刪改查操作
7.1 保存
/** * 保存一個實體 */ @Test public void testAdd() { // 定義對象 Customer c = new Customer(); c.setCustName("傳智學院"); c.setCustLevel("VIP客戶"); c.setCustSource("網路"); c.setCustIndustry("IT教育"); c.setCustAddress("昌平區北七家鎮"); c.setCustPhone("010-84389340"); EntityManager em = null; EntityTransaction tx = null; try { // 獲取實體管理對象 em = JPAUtil.getEntityManager(); // 獲取事務對象 tx = em.getTransaction(); // 開啟事務 tx.begin(); // 執行操作 em.persist(c); // 提交事務 tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.2 修改
@Test public void testMerge(){ //定義對象 EntityManager em=null; EntityTransaction tx=null; try{ //獲取實體管理對象 em=JPAUtil.getEntityManager(); //獲取事務對象 tx=em.getTransaction(); //開啟事務 tx.begin(); //執行操作 Customer c1 = em.find(Customer.class, 6L); c1.setCustName("江蘇傳智學院"); em.clear();//把c1對象從快取中清除出去 em.merge(c1); //提交事務 tx.commit(); }catch(Exception e){ //回滾事務 tx.rollback(); e.printStackTrace(); }finally{ //釋放資源 em.close(); } }
7.3 刪除
/** * 刪除 */ @Test public void testRemove() { // 定義對象 EntityManager em = null; EntityTransaction tx = null; try { // 獲取實體管理對象 em = JPAUtil.getEntityManager(); // 獲取事務對象 tx = em.getTransaction(); // 開啟事務 tx.begin(); // 執行操作 Customer c1 = em.find(Customer.class, 6L); em.remove(c1); // 提交事務 tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.4 根據id查詢
/** * 查詢一個: 使用立即載入的策略 */ @Test public void testGetOne() { // 定義對象 EntityManager em = null; EntityTransaction tx = null; try { // 獲取實體管理對象 em = JPAUtil.getEntityManager(); // 獲取事務對象 tx = em.getTransaction(); // 開啟事務 tx.begin(); // 執行操作 Customer c1 = em.find(Customer.class, 1L); // 提交事務 tx.commit(); System.out.println(c1); // 輸出查詢對象 } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } } // 查詢實體的快取問題 @Test public void testGetOne() { // 定義對象 EntityManager em = null; EntityTransaction tx = null; try { // 獲取實體管理對象 em = JPAUtil.getEntityManager(); // 獲取事務對象 tx = em.getTransaction(); // 開啟事務 tx.begin(); // 執行操作 Customer c1 = em.find(Customer.class, 1L); Customer c2 = em.find(Customer.class, 1L); System.out.println(c1 == c2);// 輸出結果是true,EntityManager也有快取 // 提交事務 tx.commit(); System.out.println(c1); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } } // 延遲載入策略的方法: /** * 查詢一個: 使用延遲載入策略 */ @Test public void testLoadOne() { // 定義對象 EntityManager em = null; EntityTransaction tx = null; try { // 獲取實體管理對象 em = JPAUtil.getEntityManager(); // 獲取事務對象 tx = em.getTransaction(); // 開啟事務 tx.begin(); // 執行操作 Customer c1 = em.getReference(Customer.class, 1L); // 提交事務 tx.commit(); System.out.println(c1); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.5 JPA中的複雜查詢
JPQL全稱Java Persistence Query Language
基於首次在EJB2.0中引入的EJB查詢語言(EJB QL),Java持久化查詢語言(JPQL)是一種可移植的查詢語言,旨在以面向對象表達式語言的表達式,將SQL語法和簡單查詢語義綁定在一起·使用這種語言編寫的查詢是可移植的,可以被編譯成所有主流資料庫伺服器上的SQL。
其特徵與原生SQL語句類似,並且完全面向對象,通過類名和屬性訪問,而不是表名和表的屬性。
7.5.1 查詢全部
//查詢所有客戶 @Test public void findAll() { EntityManager em = null; EntityTransaction tx = null; try { //獲取實體管理對象 em = JPAUtil.getEntityManager(); //獲取事務對象 tx = em.getTransaction(); tx.begin(); // 創建query對象 String jpql = "from Customer"; Query query = em.createQuery(jpql); // 查詢並得到返回結果 List list = query.getResultList(); // 得到集合返回類型 for (Object object : list) { System.out.println(object); } tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.5.2 分頁查詢
//分頁查詢客戶 @Test public void findPaged () { EntityManager em = null; EntityTransaction tx = null; try { //獲取實體管理對象 em = JPAUtil.getEntityManager(); //獲取事務對象 tx = em.getTransaction(); tx.begin(); //創建query對象 String jpql = "from Customer"; Query query = em.createQuery(jpql); //起始索引 query.setFirstResult(0); //每頁顯示條數 query.setMaxResults(2); //查詢並得到返回結果 List list = query.getResultList(); //得到集合返回類型 for (Object object : list) { System.out.println(object); } tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.5.3 條件查詢
//條件查詢 @Test public void findCondition () { EntityManager em = null; EntityTransaction tx = null; try { //獲取實體管理對象 em = JPAUtil.getEntityManager(); //獲取事務對象 tx = em.getTransaction(); tx.begin(); //創建query對象 String jpql = "from Customer where custName like ? "; Query query = em.createQuery(jpql); //對佔位符賦值,從1開始 query.setParameter(1, "傳智Podcast%"); //查詢並得到返回結果 Object object = query.getSingleResult(); //得到唯一的結果集對象 System.out.println(object); tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.5.4 排序查詢
//根據客戶id倒序查詢所有客戶 //查詢所有客戶 @Test public void testOrder() { EntityManager em = null; EntityTransaction tx = null; try { //獲取實體管理對象 em = JPAUtil.getEntityManager(); //獲取事務對象 tx = em.getTransaction(); tx.begin(); // 創建query對象 String jpql = "from Customer order by custId desc"; Query query = em.createQuery(jpql); // 查詢並得到返回結果 List list = query.getResultList(); // 得到集合返回類型 for (Object object : list) { System.out.println(object); } tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }
7.5.5 統計查詢
//統計查詢 @Test public void findCount() { EntityManager em = null; EntityTransaction tx = null; try { //獲取實體管理對象 em = JPAUtil.getEntityManager(); //獲取事務對象 tx = em.getTransaction(); tx.begin(); // 查詢全部客戶 // 1.創建query對象 String jpql = "select count(custId) from Customer"; Query query = em.createQuery(jpql); // 2.查詢並得到返回結果 Object count = query.getSingleResult(); // 得到集合返回類型 System.out.println(count); tx.commit(); } catch (Exception e) { // 回滾事務 tx.rollback(); e.printStackTrace(); } finally { // 釋放資源 em.close(); } }