Java jpa外连接查询join案例详解

作者:sfidjg 时间:2022-12-17 18:31:15 

1、IndexTagController.java


@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是一个接口,其中存在以下方法


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

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


@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


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);

}

来源:https://blog.csdn.net/qq_41708916/article/details/107795979

标签:Java,jpa,join
0
投稿

猜你喜欢

  • Java 的 FileFilter文件过滤与readline读行操作实例代码

    2022-04-09 07:22:53
  • Java学习基础之安装JDK/配置JDK环境&IEDA工具安装

    2023-02-09 13:02:40
  • Spring内存缓存Caffeine的基本使用教程分享

    2023-05-26 00:30:33
  • WPF利用WindowChrome实现自定义窗口

    2021-06-08 07:33:55
  • Spring @Transaction 注解执行事务的流程

    2022-12-30 15:41:23
  • SpringBoot+easypoi实现数据的Excel导出

    2023-04-05 12:27:19
  • flutter 中监听滑动事件

    2021-10-05 11:39:00
  • android播放器实现歌词显示功能

    2021-10-27 13:44:37
  • C# 定时器保活机制引起的内存泄露问题解决

    2022-09-19 00:31:33
  • Java调用Shell命令和脚本的实现

    2023-11-29 00:59:09
  • 详解LINQ入门(上篇)

    2023-10-15 05:57:30
  • Android图表库HelloChart绘制多折线图

    2021-06-18 05:34:45
  • 浅谈C#中的for循环与foreach循环

    2021-08-23 03:27:38
  • SpringBoot Profile多环境配置方式

    2023-12-14 01:44:24
  • 关于Java8 parallelStream并发安全的深入讲解

    2023-11-15 06:37:35
  • Android 各版本兼容性适配详解

    2021-08-23 09:20:30
  • C#正则表达式判断输入日期格式是否正确

    2022-04-20 07:31:32
  • java中ResultSet遍历数据操作

    2022-06-13 05:02:13
  • Android 创建依赖库的方法(保姆级教程)

    2023-03-12 03:55:51
  • Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

    2023-07-30 12:21:29
  • asp之家 软件编程 m.aspxhome.com