JPA实现外链接分页查询

1.Service

@Override
    public Page<News> listNews(Long id, Pageable p) {
        // 外链接 t_news t_tags
        return newsRepository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Join join = root.join("tags");

                return criteriaBuilder.equal(join.get("id"),id);
            }
        },p);
    }

2.Controller

    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size = 3, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable long id, Model model) {
        model.addAttribute("page", newsService.listNews(id,pageable));
        return "tags";
    }

3.Repository

public interface NewsRepository extends JpaRepository<News, Long>, JpaSpecificationExecutor {
}

 

©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页