Spring Data Jpa的查詢
SpringJpa的複雜查詢
-
藉助接口中定義好的方法完成查詢
/* * 統計查詢:查詢客戶的總數量 * */ @Test public void testCount(){ long count = customerDao.count(); System.out.println(count); } /* * 判斷id爲4的客戶是否存在 * */ @Test public void testExists(){ boolean b = customerDao.exists(4); System.out.println(b); } /* * 根據id從數據庫中查詢 * @Transactional:保證getOne的正常運行 * findOne:立即加載 * getOne:延遲加載,底層是getReference * */ @Test @Transactional public void testGetOne(){ Customer customer = customerDao.getOne(1); System.out.println(customer); }
-
jpql查詢方式
需要將jpql語句配置到接口方法上
- 特有的查詢,需要在dao接口上配置方法
- 在新添加的方法上,使用註解的形式配置jpql查詢語句
- 註解:@Query
根據客戶名查詢客戶
public interface CustomerDao extends JpaRepository<Customer,Integer>, JpaSpecificationExecutor<Customer> { /* * 根據客戶名查詢客戶 * jpql:from customer where name = ? * */ @Query(value = "from Customer where name = ?") public Customer findJpql(String custname); }
@RunWith(SpringJUnit4ClassRunner.class) //聲明spring提供的單元測試環境 @ContextConfiguration(locations = "classpath:applicationContext.xml") //指定spring容器的配置信息 public class JpqlTest { @Resource CustomerDao customerDao; @Test public void testFindJpql(){ Customer customer = customerDao.findJpql("黑馬"); System.out.println(customer); } }
根據客戶名和客戶id查詢客戶
/* * 根據客戶名和客戶id查詢客戶 * 對於多個佔位符,默認情況下,佔位符位置和參數的位置保持一致 * 可以指定佔位符參數的位置 * ? 索引:指定此佔位符的取值來源,例如?2:就是從方法參數中取第二個參數 * *//* * 根據客戶名和客戶id查詢客戶 * */ @Query(value = "from Customer where name = ? and id = ?") Customer findCusByNameAndId(String custname,Integer id);
@Test public void testFindCusByNameAndId(){ Customer customer = customerDao.findCusByNameAndId("黑馬程序員",3); System.out.println(customer); }
根據id更新客戶名
/* * 根據客戶id更新客戶名 * @Query:代表的是進行查詢 * @Modifying:聲明此方法是用來進行更新的 * */ @Query(value = "update Customer set name = ?2 where id = ?1") @Modifying void updCusById(Integer id,String custname);
/* * 修改、刪除需要添加事務支持,否則會報錯 * 添加了事務的支持,默認執行之後會回滾事務,使用@Rollback設置不自動回滾 * */ @Test @Transactional @Rollback(value = false) public void testUpdCusById(){ customerDao.updCusById(1,"heima"); }
-
SQL語句的查詢
需要將sql語句配置到接口方法上
-
特有的查詢,需要在dao接口上配置方法
-
在新添加的方法上,使用註解的形式配置sql查詢語句
-
註解:@Query
屬性:nativeQuery:是否使用本地查詢,true(使用本地查詢,就是sql查詢),false(不使用,默認值,也就是jpql查詢)
查詢全部客戶
/* * 查詢全部客戶 * nativeQuery:true sql查詢 | false:jpql查詢 * */ @Query(value = "select * from customer",nativeQuery = true) List<Object[]> findSql();
@Test public void testFindSql(){ List<Object[]> list = customerDao.findSql(); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
客戶名模糊查詢
/* * 客戶名模糊查詢 * */ @Query(value = "select * from customer where name like ?",nativeQuery = true) List<Object[]> findByLike(String name);
@Test public void testFindByLike(){ List<Object[]> list = customerDao.findByLike("%馬%"); for (Object[] objects : list) { System.out.println(Arrays.toString(objects)); } }
-
-
方法名稱規則查詢
是對jpql查詢更加深入的一層封裝,我們只需要按照SpringDataJpa提供的方法名稱規則定義方法,不需要再去配置jpql語句,完成查詢
根據客戶名查詢客戶
/* * 方法名的約定: * findBy:查詢 * 對象中屬性的名稱(首字母大寫),查詢的條件 * */ Customer findByName(String custname);
//測試方法命名查詢 @Test public void testFindBy(){ Customer customer = customerDao.findByName("heima"); System.out.println(customer); }
模糊查詢
/* * 模糊查詢 * */ Customer findByNameLike(String custname);
//測試模糊查詢 @Test public void testFindByNameLike(){ Customer customer = customerDao.findByNameLike("%學%"); System.out.println(customer); }
多條件查詢
//多條件查詢 Customer findByNameLikeAndId(String custname,Integer id);
//測試多條件查詢 @Test public void testFindByNameLikeAndId(){ Customer customer = customerDao.findByNameLikeAndId("%學%",2); System.out.println(customer); }