zoukankan      html  css  js  c++  java
  • spring JPA分页排序条件查询

    @RequestMapping("/listByPage")
        public Page<Production> listByPage(int page, int size, String sortStr, boolean sortAscOrDesc, String searchObj)
        {
            Specification<Production> specification = getSpecification(searchObj);
    
            if (StringUtils.isEmpty(sortStr))
            {
                return productionRepository.findAll(specification, new PageRequest(page, size));
            }
            else
            {
                return productionRepository.findAll(specification, new PageRequest(page, size, new Sort((sortAscOrDesc ? Sort.Direction.ASC : Sort.Direction.DESC), sortStr)));
            }
        }
    
        private Specification getSpecification(String searchObj)
        {
            JSONObject search = (JSONObject) JSONValue.parse(searchObj);
    
            Specification<Production> specification = new Specification<Production>()
            {
                @Override
                public Predicate toPredicate(Root<Production> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder)
                {
                    List<Predicate> queryList = new ArrayList<Predicate>();
    
                    if (search.get("name") != null && !StringUtils.isEmpty(search.get("name") + ""))
                    {
                        queryList.add(criteriaBuilder.equal(root.get("name"), search.get("name") + ""));
                    }
                    if (search.get("name1") != null && !StringUtils.isEmpty(search.get("name1") + ""))
                    {
                        queryList.add(criteriaBuilder.equal(root.get("name1"), search.get("name1") + ""));
                    }
    
                    if (queryList.size() > 0)
                    {
                        criteriaQuery.where(queryList.toArray(new Predicate[queryList.size()]));
                    }
    
                    return criteriaQuery.getRestriction();
                }
            };
    
            return specification;
        }
  • 相关阅读:
    多窗体
    滚动条
    个人信息调查
    登录页面
    蓝桥杯——放麦子
    java的BigDecimal
    蓝桥杯——判定字符的位置。
    输出日历
    蓝桥杯---简单的计算器
    蓝桥杯--Quadratic Equation
  • 原文地址:https://www.cnblogs.com/white-knight/p/8650432.html
Copyright © 2011-2022 走看看