JpaSpecificationExecutor实现关联查询

JpaSpecificationExecutor实现关联查询

记录一次错误

在写一个项目的时候发现值一直无法传到前台,仔细查看原来是排序规则写错了,项目用的是layui框架:
后台:

public interface BookDao extends JpaRepository , JpaSpecificationExecutor {
    Page findAll(Specification specification, Pageable pageable);
}

继承JpaSpecificationExecutor接口

  public LayuiPage showBookList(@RequestParam(name ="name",required = false)String name,
                                        @RequestParam(name = "page", required = false, defaultValue = "0") int pageNum,
                                        @RequestParam(name = "limit", required = false, defaultValue = "20") int pageSize) {
        return new LayuiPage<>(bookDao.findAll((Specification) (root, criteriaQuery, cb) -> {
            List predicates = new ArrayList<>();
            if (!StringUtils.isEmpty(name)) predicates.add(cb.or(cb.like(root.get("bookName").as(String.class), "%" + name + "%"),
                        cb.like(root.get("bookAuthor").as(String.class), "%" + name + "%")));
            Predicate[] aryPredicates = new Predicate[predicates.size()];
            criteriaQuery.where(predicates.toArray(aryPredicates));
            return cb.and(aryPredicates);
        }, PageRequest.of(pageNum > 0 ? pageNum - 1 : 0, pageSize, new Sort(Sort.Direction.ASC, "bookId"))));
    }

Layui是自己写的页面返回类
过程中把排序规则写错了,应该为实体类的ID而非表中的字段

你可能感兴趣的:(报错,spring)