我是靠谱客的博主 魔幻钢笔,这篇文章主要介绍Java jpa外连接查询join案例详解,现在分享给大家,希望可以做个参考。

1、IndexTagController.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@GetMapping("/tags/{id}") public String types(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable, @PathVariable long id, Model model, HttpSession session){ //找到所有的标签,并且按照标签新闻量排序 List<Tag> tags = tagService.listTagTop(50); if(id == -1){ //得到最大数据量的分类 id = tags.get(0).getId(); } model.addAttribute("tags",tags); model.addAttribute("page",newsService.listNews(id,pageable)); model.addAttribute("activeId",id); session.setAttribute("query",""); return "tags"; }

newService.listNews(id,pgeable)中id为标签的id,这个方法要做的就是查询出标签中包含id为参数id的所有新闻。

2、业务层代码

NewService.java是一个接口,其中存在以下方法

复制代码
1
2
//根据标签Id查找符合条件的新闻 Page<News> listNews(long id,Pageable pageable);

NewServiceImpl.java为实现NewService接口的类,实现listNews方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
@Override public Page<News> listNews(long id, Pageable pageable) { return newsRepository.findAll(new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { //外连接查询 Join Join join =root.join("tags"); return cb.equal(join.get("id"),id); } },pageable); }

NewsRepository.java 继承了JpaSpecificationExecutor

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface NewsRepository extends JpaRepository<News,Long>, JpaSpecificationExecutor { @Query("select n from News n where n.recommend = true ") List<News> findTop(Pageable pageable); @Query("select n from News n where n.title like ?1 or n.content like ?1") Page<News> findByQuery(String query,Pageable pageable); @Query("select function('date_format',n.updateTime,'%Y') as year1 from News n group by year1 order by year1 desc ") List<String> findGroupYear(); @Query("select n from News n where function('date_format',n.updateTime,'%Y') = ?1 ") List<News> findByYear(String year); }

到此这篇关于Java jpa外连接查询join案例详解的文章就介绍到这了,更多相关Java jpa外连接查询join内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是魔幻钢笔最近收集整理的关于Java jpa外连接查询join案例详解的全部内容,更多相关Java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部