• JPA——根据条件分页查询实现方法


    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.jpa.domain.Specification;
    import org.springframework.data.jpa.domain.Specifications;
    import org.springframework.stereotype.Service;
    
    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Predicate;
    import javax.persistence.criteria.Root;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * TODO
     * 下载记录
     */
    @Service
    public class ExcelDownloadServiceImpl {
    
        @Autowired
        ExcelDownloadRepo excelDownloadRepo;
    
        /*
        分页查询
         */
        public Page<ExcelDownload> getMore(PageQuery query, ExcelDownload excelDownload) {
            PageRequest pageable = new PageRequest(query.getPindex(), query.getPcount(), query.getSortObj());
            Specification<ExcelDownload> spe = getSpecification(excelDownload);
            Page<ExcelDownload> page = excelDownloadRepo.findAll(Specifications.where(spe), pageable);
            return page;
        }
    
        public Specification<ExcelDownload> getSpecification(ExcelDownload excelDownload) {
            Specification<ExcelDownload> spe = new Specification<ExcelDownload>() {
                @Override
                public Predicate toPredicate(Root<ExcelDownload> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    List<Predicate> list = new ArrayList<Predicate>();
    
                    if (StringUtils.isNotBlank(excelDownload.getModule())) {
                        list.add(cb.equal(root.get("module").as(String.class), excelDownload.getModule()));
                    }
                    if (StringUtils.isNotBlank(excelDownload.getDownloadState())) {
                        list.add(cb.equal(root.get("downloadState").as(String.class), excelDownload.getDownloadState()));
                    }
                    if(null != excelDownload.getStartDate()){
                        list.add(cb.greaterThanOrEqualTo(root.<Date> get("createTime"), excelDownload.getStartDate()));
                    }
                    if(null != excelDownload.getEndDate()){
                        list.add(cb.lessThanOrEqualTo(root.get("createTime"),excelDownload.getEndDate()));
                    }
    
                    Predicate[] p = new Predicate[list.size()];
                    query.where(cb.and(list.toArray(p)));
                    query.orderBy(cb.desc(root.get("id").as(Long.class)));
                    return query.getRestriction();
                }
            };
            return spe;
        }
    }
    

      

  • 相关阅读:
    HDU1852 Beijing 2008(快速幂+特殊公式)
    HihoCoder 1570 : 小Hi与法阵(简单几何)
    【转】反素数
    【整理】线段树30题
    SPOJcot2 Count on a tree II (树上莫队)
    【总结】曼哈顿距离转切比雪夫距离
    【初识】树上分块
    基于Tablestore Tunnel的数据复制实战
    【New Feature】阿里云快照服务技术解析
    基于日志服务的GrowthHacking(1):数据埋点和采集(APP、Web、邮件、短信、二维码埋点技术)
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/14635904.html
Copyright © 2020-2023  润新知