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

项目中经常会使用到一对多的查询场景,但是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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • mybatis使用pageHelper插件进行查询分页

    在数据库服务器中,sql语句实现分页便要每个查询语句都要写上limit(开始,结束),并且不能灵活的随前端变化,为此使用拦截器的方法,过程:拦截器拦截请求的sql语句(根据需要拦截的ID(正则匹配),进行拦截),并对根据前端传过来的页数,和每页的条数,计算出limit(开始,结束),总条数,然后,拼接到sql语句后边.其中这个处理过程,已经封装到了,分页插件中,可以不用理解,直接使用. mybatis查询分页---使用pageHelper插件 之前在spring+springmvc由于整个大多都

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

    项目中经常会使用到一对多的查询场景,但是PageHelper对这种嵌套查询的支持不够,如果是一对多的列表查询,返回的分页结果是不对的 参考Github上的说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md 对于一对多的列表查询,有两种方式解决 1.在代码中处理.单独修改分页查询的resultMap,删除collection标签,然后在代码中遍历结果,查询子集 2.使用mybat

  • ssm框架+PageHelper插件实现分页查询功能

    通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询. 源码:https://gitee.com/smfx1314/pagehelper 看一下项目结构: 首先创建一个maven工程,pom中引入相关jar包 <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId&

  • pagehelper插件显示total为-1或1的问题

    简单讲下用法: //引依赖 <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>com.github.pagehelper<

  • 使用PageHelper插件实现Service层分页

    本文实例为大家分享了使用PageHelper插件实现Service层分页的具体代码,供大家参考,具体内容如下 使用场景: 平时分页我们可以直接使用mybatis-plus中内置的IPage进行分页,一般是在mapper中写好接口,在执行sql时就将其进行分页操作,但是有些复杂的查询或者是需要拼接返回格式的数据就难以操作了,所以我们使用PageHelper插件来实现Service分页功能. 1.在pom.xml文件中导入PageHelper插件依赖 <!--pagehelper分页插件--> &

  • mybatis使用pagehelper插件过程详解

    这篇文章主要介绍了mybatis使用pagehelper插件过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.添加插件的依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.4</version> <

  • SpringBoot整合mybatis结合pageHelper插件实现分页

    SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看官方文档: https://pagehelper.github.io/ 1.使用前配置 关于pageHelper的使用配置,主要有以下2个步骤: 1.1.在pom文件中导入pageHelper依赖 <dependency> <groupId>com.github.pagehelper&

  • 解决mybatis一对多查询resultMap只返回了一条记录问题

    问题描述:因为领导的一个需求,需要用到使用resultMap,很久没使用了,结果就除了点意外.就记录下这个问题 准备两个类:author(作者)和book(书),数据库创建对应的author->book一对多的数据 @Data public class Author { private Integer id; private String name; private String phone; private String address; private List<Book> book

  • 基于mybatis一对多查询内层排序的问题

    目录 mybatis一对多查询内层排序 mybatis多排序问题 mybatis一对多查询内层排序 <!--根据板块id查询所有主题->指标->维度--> <resultMap id="TitleDimensionMap" type="com.etouch.admincenter.bean.ZhmdDiagnosisTitleBean"> <id column="title_id" property=&q

  • Mybatis查询时数据丢失的问题及解决

    目录 Mybatis查询时数据丢失 经过排查得出结论 解决办法 Mybatis查询部分字段漏查问题(mysql) Mybatis查询时数据丢失 公司里的实体类和mapper文件均由mybatis逆向工程生成 之前使用myabtis查询时直接使用注解@select(......)时遇到了一个问题. 结果显示数据库查询没有问题,但是有的数据缺没有插入到指定的字段中,如下图中ID成功存储,Z40_ID,Z40_103到Z40_113均失败. 经过排查得出结论 如果数据库命名很规范比如user_name

随机推荐