public interface DeviceRepository extends JpaRepository<Device,String>,JpaSpecificationExecutor {

}

 

2.重写toPredicate 方法

 public Page findDeviceByParams(int pageNo, int pageSize, String rIndexCode, String name, String parentDevice) {

        try{
            Specification specification = new Specification() {
                @Override
                public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicateList = new ArrayList<>();
                    if(!StringUtils.isEmpty(name)){
                        predicateList.add(criteriaBuilder.like(root.get("name"),"%" + name + "%"));
                    }
                    if(!StringUtils.isEmpty(parentDevice)){
                        predicateList.add(criteriaBuilder.like(root.get("parentDevice"),"%" + parentDevice + "%"));

                    }
                    predicateList.add(criteriaBuilder.equal(root.get("rIndexCode"),rIndexCode));
                    return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
                }
            };

            Sort sort = new Sort(Sort.Direction.ASC,"indexCode");
            PageRequest pageRequest = new PageRequest(pageNo-1,pageSize,sort);
            return deviceRepository.findAll(specification,pageRequest);
        }catch(Exception e){
            logger.error("查询数据库出错:" + e);
            return null;
        }


    }

   springdata jpa 实现and or 组合查询

  https://blog.csdn.net/langyan122/article/details/80608383

  https://blog.csdn.net/weixin_42475367/article/details

 springdata jpa Example查询

 

相关文章:

  • 2021-10-19
  • 2021-09-24
  • 2021-08-06
  • 2022-12-23
  • 2021-11-24
  • 2021-11-13
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2021-07-07
  • 2021-08-24
  • 2021-11-23
  • 2021-06-26
  • 2021-09-13
  • 2021-09-13
  • 2021-09-03
相关资源
相似解决方案