2019-02-26

2020-01-15  本文已影响0人  MartinX_c56e

                            spring JPA 查询-分页方法以及模糊查询的一些记录

                      1:简单的分页查询

                        Sort sorts =new Sort( Sort.Direction.DESC, "createTime");

    Sort sorts =new Sort( Sort.Direction.DESC, "createTime");

Pageable pageable = PageRequest.of(pageNum -1, pageSize, sorts);

Page pages=merchantOrderRepository.findAll((Specification) (root, query, cb) -> {

List list =new ArrayList<>();

    if (null !=serviceType) {

Expression exp = root.get("serviceType");

        List serviceTypeList =new ArrayList<>();

        if(serviceType ==1){

serviceTypeList.add("0");

            serviceTypeList.add("1");

        }else if(serviceType ==2){

serviceTypeList.add("2");

        }

list.add(exp.in(serviceTypeList));

    }

if (null !=orderStatus) {

list.add(cb.equal(root.get("status"), orderStatus));

    }

if (!ObjectUtils.isEmpty(technician.getId())) {

list.add(cb.equal(root.get("technicianId"), technician.getId()));

    }

Predicate[] p =new Predicate[list.size()];

    return cb.and(list.toArray(p));

}, pageable);

2:自带模糊查询

Sort sorts =new Sort(Sort.Direction.DESC, "createTime");

PageRequest pageRequest =new PageRequest(page -1, pageSize, sorts);

//分页查询

Page merchants =merchantReposotory.findAll(new Specification() {

@Override

    public PredicatetoPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {

Predicate predicate = cb.conjunction();

        if (merchant !=null){

if (!StringUtils.isEmpty(merchant.getName())){

predicate.getExpressions().add(cb.like(root.get("name"),"%"+merchant.getName()+"%"));

            }

if (null !=merchant.getStatus()) {

predicate.getExpressions().add(cb.equal(root.get("status"), merchant.getStatus()));

            }

if (null !=merchant.getBusinessStatus()) {

predicate.getExpressions().add((cb.equal(root.get("businessStatus"), merchant.getBusinessStatus())));

            }

if (!StringUtils.isEmpty(merchant.getProvinceId())) {

predicate.getExpressions().add((cb.equal(root.get("provinceId"), merchant.getProvinceId())));

            }

if (!StringUtils.isEmpty(merchant.getCityId())) {

predicate.getExpressions().add(cb.equal(root.get("cityId"), merchant.getCityId()));

            }

}

return predicate;

    }

}, pageRequest);

上一篇下一篇

猜你喜欢

热点阅读