【JPA】Spring Data JPAは改ページと条件照会を実現します.

20732 ワード

記事の目次
  • 、`Repository`階で2つのインターフェース
  • を継承します.
  • 、Service層でのクエリー動作
  • 、Pageの方法
  • 1、Repository層で2つのインターフェースを継承する.
  • JpaRepository汎型パラメータ:1.照会するエンティティ、2.このエンティティのメインキータイプ
  • JpaSpecification Exector汎型パラメータ:要チェックエンティティ
  • @Repository
    public interface AdminRepository extends JpaRepository<Admin, Integer>, JpaSpecificationExecutor<Admin> {
    }
    
    2、サービス層で照会操作を行う
    Queryはカスタムクラスで、検索の条件を置いています.
    public Page<Book> findByPage(Query query) {
            //1.  Page  ,  1:   (   0  ),  2:       
            PageRequest pageRequest = PageRequest.of(query.getPageNum() - 1, query.getPageSize());
            /**
            *	2.      ,  
            */
            Specification<Book> specification = new Specification<Book>() {
                @Override
                public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                    //          ,       List 
                    ArrayList<Predicate> predicateList = new ArrayList<>();
                	//       like   
                    if (!StringUtils.isEmpty(query.getName())) {
                        Predicate name = cb.like(root.get("name"), "%" + query.getName() + "%");
                        predicateList.add(name);
                    }
                    //       like   
                    if (!StringUtils.isEmpty(query.getAuthor())) {
                        Predicate author = cb.like(root.get("author"), "%" + query.getAuthor() + "%");
                        predicateList.add(author);
                    }
                    //       like   
                    if (!StringUtils.isEmpty(query.getPublishHouse())) {
                        Predicate publishHouse = cb.like(root.get("publishHouse"), "%" + query.getPublishHouse() + "%");
                        predicateList.add(publishHouse);
                    }
                    //        like   
                    if (!StringUtils.isEmpty(query.getType())) {
                        Predicate type = cb.like(root.get("type"), "%" + query.getType() + "%");
                        predicateList.add(type);
                    }
                    //        between   
                    if (query.getMinPrice() < query.getMaxPrice()) {      
                    	//    price  getMinPrice   getMaxPrice   
                        Predicate price = cb.between(root.get("price"), query.getMinPrice(), query.getMaxPrice());
                        predicateList.add(price);
                    }
    				//            
    				if (query.getMinPrice() == 0 && query.getMaxPrice() == 0) {
                    	//     price >=getMinPrice()
                        Predicate greaterEqual = cb.greaterThanOrEqualTo(root.get("price"), query.getMinPrice());
                        predicateList.add(greaterEqual);
                    } 
    					
    				//           predicateList        ,                   
                    Predicate[] predicates = new Predicate[predicateList.size()];
                    //     OR    and  
                    if ("or".equals(query.getSharpness())) {
                    	//                        
                        return cb.or(predicateList.toArray(predicates));
                    } else {
                    	//                       
                        return cb.and(predicateList.toArray(predicates));
                    }
                }
            };
            //    findAll   ,     :     ,     :    
            //  :        bookRepository     JpaSpecificationExecutor  (       )
            Page<Book> page = bookRepository.findAll(specification, pageRequest);
            //  page  
            return page;
        }
    
    3、Pageの方法
    // Controller   Service    ,  Page  
    Page<Book> page = bookService.findByPage(query);
    //         
    boolean b = page.hasContent();
    //       List  
    List<Book> content = page.getContent();
    //       (          ,         )
    long totalElements = page.getTotalElements();
    //   
    int totalPages = page.getTotalPages();