Spring DataJPA的Specifications動態查詢

Spring DataJPA的Specifications動態查詢

JpaSpecificationExecutor接口的方法
T findOne(Specification<T> var1);  //查詢單個對象

List<T> findAll(Specification<T> var1);  //查詢列表
//查詢全部 分頁  Pageable:分頁參數
//返回值:分頁PageBean(springdatajpa提供的)
Page<T> findAll(Specification<T> var1, Pageable var2);
//查詢列表,
//Sort:排序參數
List<T> findAll(Specification<T> var1, Sort var2);
//統計查詢
long count(Specification<T> var1);
Specification對象:查詢條件(進行查詢時,需要自定義我們自己的Specification實現類)

​ 實現的方法:

/*
* Root:代表查詢的跟對象(查詢的任何屬性都可以從跟對象中獲取)
* CriteriaQuery:頂層查詢對象,自定義查詢方式(瞭解)
* CriteriaBuilder:查詢的構造器,封裝了很多查詢條件
*/
Predicate toPredicate(Root<T> var1, CriteriaQuery<?> var2, CriteriaBuilder var3);//封裝查詢條件
dao層接口
public interface CustomerDao extends JpaRepository<Customer,Integer>, JpaSpecificationExecutor<Customer> {

}
根據客戶名查詢客戶
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpecTest {

    @Resource
    CustomerDao customerDao;

    /*
     * 根據條件,查詢單個對象
     * */
    @Test
    public void testSpec(){
        //匿名內部類
        /*
        * 定義一查詢條件,
        *   1.實現Specification接口(提供泛型,就是查詢的對象類型)
        *   2.實現toPredicate方法(構造查詢條件)
        *   3.需要藉助方法參數中的兩個參數(root:獲取需要查詢的對象屬性;CriteriaBuilder:構造查詢條件的,內部封裝了很多查詢條件(模糊匹配,精準匹配))
        *
        * 案例:根據客戶名查詢
        *   查詢條件:
        *       1.查詢方式,在CriteriaBuilder對象中
        *       2.比較的屬性名稱:在Root對象中
        * */
        Specification<Customer> spec = new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //獲取比較的屬性
                Path<Object> name = root.get("name");
                //構造查詢條件,select * from customer where name = "heima"
                //參數1:需要比較的屬性Path對象 參數2;當前需要比較的取值
                Predicate predicate = cb.equal(name, "heima");//進行精準匹配,比較屬性的取值
                return predicate;
            }
        };
        Customer customer = customerDao.findOne(spec);
        System.out.println(customer);
    }
}
多個條件進行查詢(接口內容不需要寫)
/*
* 多條件查詢:根據客戶名和客戶所屬行業進行查詢
*  */
@Test
public void testSepc2(){
/*
* root:獲取屬性:客戶名稱,所屬行業
* cb:構造查詢
*   1.構造客戶名的精準匹配查詢
*   2.構造所屬行業的精準匹配查詢
*   3.將以上兩個查詢聯繫起來
* */
Specification<Customer> spec = new Specification<Customer>() {
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
//獲取客戶名稱,所屬行業
Path<Object> name = root.get("name");
Path<Object> industry = root.get("industry");
//.構造客戶名的精準匹配查詢
Predicate predicate1 = cb.equal(name, "heima");
Predicate predicate2 = cb.equal(industry, "教育");
//將以上兩個查詢聯繫起來(組合:滿足條件1並且滿足條件2,滿足條件1或者滿足條件2)
Predicate predicate = cb.and(predicate1, predicate2);  //以與的形式拼接條件
//cb.or()  以或的方式拼接條件
return predicate;
}
};
Customer customer = customerDao.findOne(spec);
System.out.println(customer);
}
根據用戶名進行模糊查詢
/*
    * 案例:根據客戶名進行迷糊查詢
    * equal:直接得到path對象(屬性),然後進行比較即可
    * gt lt le like:得到path對象,根據path指定比較的參數類型,再去進行比較 path.as(參數類型的字節碼)
    * */
@Test
public void testSpec3(){
    Specification<Customer> spec = new Specification<Customer>() {
        public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
            Path<Object> name = root.get("name");
            Predicate predicate = cb.like(name.as(String.class), "%馬%");
            return predicate;
        }
    };
    List<Customer> customerList = customerDao.findAll(spec);
    System.out.println(customerList);
}
根據客戶名進行迷糊查詢並按照id排序
/*
     * 案例:根據客戶名進行模糊查詢並按照id排序
     * */
@Test
public void testSpec4(){
    Specification<Customer> spec = new Specification<Customer>() {
        public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
            Path<Object> name = root.get("name");
            Predicate predicate = cb.like(name.as(String.class), "%馬%");
            return predicate;
        }
    };
    //創建排序對象,需要調用構造方法實例化對象
    //參數1:排序的順序(Sort.Direction.DESC:倒序 Sort.Direction.ASC:升序);參數2:排序的屬性名
    Sort sort = new Sort(Sort.Direction.DESC,"id");
    List<Customer> customerList = customerDao.findAll(spec,sort);
    System.out.println(customerList);
}
分頁查詢
/*
* 分頁查詢
* findAll(Specification,Pageable):帶有條件的分頁
* findAll(Pageable):沒有條件的分頁
*   參數1:Specification
*   參數2:Pageable
*       查詢的頁碼,每頁查詢的條數
*
* 返回:Page對象:SpringDataJpa爲我們封裝好的pageBean對象
* */
@Test
public void testSpec5(){
/*
* PageRequest樹Pageable接口的實現類
* 創建PageRequest時需要調用構造方法傳入兩個參數
*   參數1:當前查詢的頁數(從0開始)
*   參數2:當前查詢的總數
* */
Pageable pageable = new PageRequest(0,3);
//分頁查詢
Page<Customer> page = customerDao.findAll(pageable);
System.out.println(page.getTotalElements());  //表中的總條數
System.out.println(page.getTotalPages());   //得到總頁數
System.out.println(page.getContent());   //得到結果列表,list集合
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章