PageHelper插件实现一对多查询时的分页问题

作者:JouyPub 时间:2021-11-05 07:02:34 

项目中经常会使用到一对多的查询场景,但是PageHelper对这种嵌套查询的支持不够,如果是一对多的列表查询,返回的分页结果是不对的

参考Github上的说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md

对于一对多的列表查询,有两种方式解决

1、在代码中处理。单独修改分页查询的resultMap,删除collection标签,然后在代码中遍历结果,查询子集

2、使用mybatis提供的方法解决,具体如下

定义两个resultMap,一个给分页查询使用,一个给其余查询使用


<resultMap id="BaseMap" type="com.xx.oo.Activity">
 <id column="id" property="id" jdbcType="INTEGER"/>
   ....
</resultMap>

<resultMap id="ResultMap" type="com.xx.oo.Activity" extends="BaseMap">
 <collection property="templates" ofType="com.xx.oo.Template">
   <id column="pt_id" property="id" jdbcType="INTEGER"/>
   <result column="pt_title" property="title" jdbcType="VARCHAR"/>
 </collection>
</resultMap>

<resultMap id="RichResultMap" type="com.xx.oo.Activity" extends="BaseMap">
 <!--property:对应JavaBean中的字段-->
 <!--ofType:对应JavaBean的类型-->
 <!--javaType:对应返回值的类型-->
 <!--column:对应数据库column的字段,不是JavaBean中的字段-->
 <!--select:对应查询子集的sql-->
 <collection property="templates" ofType="com.xx.oo.Template" javaType="java.util.List" column="id" select="queryTemplateById">
   <id column="pt_id" property="id" jdbcType="INTEGER"/>
   <result column="pt_title" property="title" jdbcType="VARCHAR"/>
 </collection>
</resultMap>

<resultMap id="template" type="com.xx.oo.Template">
 <id column="pt_id" property="id" jdbcType="INTEGER"/>
 <result column="pt_title" property="title" jdbcType="VARCHAR"/>
</resultMap>

需要分页的查询,使用RichResultMap。先定义一个查询子集的sql


<!--这里的#{id}参数就是collection中定义的column字段-->
<select id="queryTemplateById" parameterType="java.lang.Integer" resultMap="template">
 select id pt_id, title pt_title
 from t_activity_template where is_delete=0 and activity_id = #{id}
 order by sort_number desc
</select>
<select id="queryByPage" parameterType="com.xx.oo.ActivityPageRequest" resultMap="RichResultMap">
 SELECT t.*,t1.real_name creator_name
 FROM t_activity t
 left join user t1 on t1.user_id = t.creator
 <where>
   t.is_delete = 0
   <if test="criteria != null and criteria.length()>0">AND (t.activity_name like concat("%",#{criteria},"%"))</if>
 </where>
 ORDER BY t.id desc
</select>

不需要分页的普通查询,使用ResultMap


<select id="queryById" parameterType="java.lang.Integer" resultMap="ResultMap">
 SELECT t.*, t6.id pt_id, t1.title pt_title
 FROM t_activity t
 left join t_activity_template t1 on t.id=t6.activity_id and t1.is_delete=0
 WHERE t.is_delete = 0 AND t.id = #{id}
</select>

来源:https://segmentfault.com/a/1190000018825136

标签:PageHelper,查询,分页
0
投稿

猜你喜欢

  • Java数据结构之数组(动力节点之Java学院整理)

    2023-09-23 21:20:12
  • Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码

    2023-11-23 08:23:43
  • 实例讲解JAVA设计模式之备忘录模式

    2023-08-29 16:31:19
  • Java编程实现获取当前代码行行号的方法示例

    2021-06-28 06:51:14
  • java堆排序概念原理介绍

    2021-08-30 12:31:58
  • 基于java中的流程控制语句总结(必看篇)

    2023-11-08 09:56:59
  • Jenkins节点配置实现原理及过程解析

    2023-02-14 11:53:30
  • Java调用Shell命令和脚本的实现

    2023-11-29 00:59:09
  • Java中final修饰的方法是否可以被重写示例详解

    2022-02-25 13:35:40
  • Spring Boot常用注解(经典干货)

    2023-11-24 22:29:10
  • maven多profile 打包下 -P参和-D参数的实现

    2023-03-22 08:25:08
  • 关于spring的自定义缓存注解分析

    2023-11-28 17:02:50
  • Eureka源码阅读之环境搭建及工程结构

    2023-07-26 01:02:36
  • Android 消息机制以及handler的内存泄露

    2023-08-01 07:59:44
  • IDEA SpringBoot项目配置热更新的步骤详解(无需每次手动重启服务器)

    2023-11-12 00:22:41
  • 解析springboot整合谷歌开源缓存框架Guava Cache原理

    2023-11-07 13:24:23
  • Kotlin中的惰性操作容器Sequence序列使用原理详解

    2023-10-01 14:21:55
  • java抓取网页或文件中的邮箱号码

    2023-07-30 19:19:28
  • 详解Struts2动态方法调用

    2022-10-18 11:19:25
  • Android应用内悬浮窗Activity的简单实现

    2023-07-28 04:03:00
  • asp之家 软件编程 m.aspxhome.com