zoukankan      html  css  js  c++  java
  • spring data jpa 参数为null查询所有,否则根据参数查询

     分页结果集封装

    public class PageResult<T> {
        private long total;//总条数
        private Integer totalPage;//总页数
        private List<T> list;
    
        public PageResult() {
        }
    
        public PageResult(long total, List<T> list) {
            this.total = total;
            this.list = list;
        }
    
        public PageResult(long total, Integer totalPage, List<T> list) {
            this.total = total;
            this.totalPage = totalPage;
            this.list = list;
        }
    
        public long getTotal() {
            return total;
        }
    
        public void setTotal(long total) {
            this.total = total;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public List<T> getList() {
            return list;
        }
    
        public void setList(List<T> list) {
            this.list = list;
        }
    }
    

      

    
    
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result getBrandList(@RequestBody Map<String,String> map ){
    String search = map.get("search");
    String pageNum = map.get("pageNum");
    String pageSize = map.get("pageSize");
    if (StringUtils.isEmpty(pageNum)){
    pageNum="1";
    }
    if (StringUtils.isEmpty(pageSize)){
    pageSize="5";
    }
    PageResult<Brand> list=brandService.getBrandList(search,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
    return ResultUtil.success(list);
    }
     
     PageResult<Brand> getBrandList(String search, Integer pageNum, Integer pageSize);
    @Autowired
        private BrandDao brandDao;
    
        @Override
        public PageResult<Brand> getBrandList(String search, Integer pageNum, Integer pageSize) {
            Specification<Brand> specification=new Specification<Brand>() {
    
                //select * from tb_brand where name like?  limit 0,10;
                @Override
                public Predicate toPredicate(Root<Brand> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    Path<Object> name = root.get("name");
                    List<Predicate> predicateList = new ArrayList<>();
                    if (search!=null){
                        Predicate  p1 = criteriaBuilder.like(name.as(String.class),"%"+search+"%");
                        predicateList.add(p1);
                        return criteriaQuery.where(p1).getRestriction();
                    }
                    Predicate[] pre = new Predicate[predicateList.size()];
                    return criteriaQuery.where(predicateList.toArray(pre)).getRestriction();
                }
            };
    
            Pageable pageable=new PageRequest(pageNum-1,pageSize);
            Page<Brand> page=brandDao.findAll(specification,pageable);
            List<Brand> list = page.getContent();//数据
            int totalPages = page.getTotalPages();//总页数
            long total = page.getTotalElements();//总条数
    
            return new PageResult<>(total,totalPages,list);
        }
    

      

    public interface BrandDao extends JpaRepository<Brand,Long>, JpaSpecificationExecutor<Brand> {
    
    }

     

  • 相关阅读:
    POJ 2135 Farm Tour(最小费用最大流,变形)
    HDU 1503 Advanced Fruits (LCS,变形)
    HDU Senior's Gun (水题)
    POJ 3648 Wedding (2-SAT,经典)
    HDU 3549 Flow Problem 流问题(最大流,入门)
    解决在eclipse中导入项目名称已存在的有关问题
    如何将js导入时的小红叉去掉
    servlet传值到servlet传值问题
    转发重定向的用法
    parameter与attribute的使用场合(转载自草原和大树)
  • 原文地址:https://www.cnblogs.com/yscec/p/12149941.html
Copyright © 2011-2022 走看看