1.使用ExampleMatcher

ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("userId", match -> match.exact()) //精确匹配userId
                .withIgnorePaths("id");//忽略属性:是否关注。因为是基本类型,需要忽略掉
ShareRecord shareRecord = new ShareRecord();
shareRecord.setUserId(userId);
Example<ShareRecord> of = Example.of(shareRecord, matcher);
pageable  = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort());
Page<ShareRecord> page = shareRecordRepository.findAll(of,pageable);

2.使用Specification

Page<ShareRecord> page = shareRecordRepository.findAll(new Specification<ShareRecord>(){
   @Override
   public Predicate toPredicate(Root<ShareRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
       List<Predicate> list = new ArrayList<>();
          if (StringUtils.isNotEmpty(userId))
              list.add(cb.equal(root.<String>get("userId"), userId));
          if (list.size() != 0) {
              Predicate[] p = new Predicate[list.size()];
              return cb.and(list.toArray(p));
          } else {
              return null;
          }
       }
}, new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort()));

相关文章:

  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2021-10-16
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
猜你喜欢
  • 2021-07-20
  • 2021-10-19
  • 2021-12-18
  • 2023-03-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案