使用Mybatis分页插件PageHelper时的分页问题

使用Mybatis分页插件PageHelper时的分页问题

1对1查询,分页正常

1对多查询,如使用左右连接查询则会导致结果的总记录条数,子记录条数会叠加到主记录条数,导致数据不对称。

总结:使用mybatis时,在一对多的查询并且需要分页的时候需要使用子查询形式。

  1) 主记录的resultMap  <resultMap id="artWorkMap" type="Artwork">
        <id column="id" jdbcType="VARCHAR" property="id"></id>
        <result column="commited" jdbcType="VARCHAR" property="commited"></result>
        <association property="user">
            <id column="u_id" jdbcType="VARCHAR" property="id"></id>
            <result column="u_username" jdbcType="VARCHAR" property="username"></result>
        </association>
        <association property="template" resultMap="templateRigMap">

        </association>
        <collection property="materialCompositions" ofType="MaterialComposition"                    select="selectMaterialCompositions" ### 关联的子查询             column="id">  ### 传主记录的列名的参数值

        </collection>
    </resultMap>

  2) 子记录的ResultMap,依次为 "1对1" 的与 "1对多" 的
    <resultMap id="templateRigMap" type="Template">        <id column="bt_id" jdbcType="VARCHAR" property="id"></id>        <result column="bt_template_name" jdbcType="VARCHAR" property="templateName"></result>        <result column="bt_deleted" jdbcType="BIT" property="deleted"></result>        <result column="bt_create_date" jdbcType="TIMESTAMP" property="createDate"></result>    </resultMap>    
    <resultMap id="materialCompositionsRigMap" type="MaterialComposition">        <id column="bmc_id" jdbcType="VARCHAR" property="id"></id>        <result column="bmc_artwork_id" jdbcType="VARCHAR" property="artworkId"></result>    </resultMap>    3)主记录查询 (把一对一的查询放在一起,一对多的数据另写一条查询)
    <select id="selectAllOrInCompany" resultMap="artWorkMap">        SELECT         ba.*

        ,su.id "u_id"        ,su.username "u_username"

        ,bc.id        ,bc.company_no        ,bc.update_date "c_update_date"

        ,bb.id "bb_id"        ,bb.update_date "bb_update_date"

        ,bt.id "bt_id"        ,bt.update_date "bt_update_date"

        from biz_artwork ba        left join sys_user su on su.id = ba.user_id        left join biz_company bc on bc.id = su.company_id        left join base_brand bb on ba.brand_id = bb.id        left join base_template bt on ba.template_id = bt.id        where ba.deleted = 0    <choose>        <when test="companyNo !=null and companyNo != ‘‘">            and ba.artwork_no like concat(‘‘, #{companyNo}, ‘%‘)        </when>        <otherwise>            and ba.commited = 1        </otherwise>    </choose>     <if test="keywords !=null and keywords.length() > 0">         and (         ba.artwork_no like concat(‘%‘, #{keywords}, ‘%‘)         or su.username like concat(‘%‘, #{keywords}, ‘%‘)         or bc.company_name like concat(‘%‘, #{keywords}, ‘%‘)         )</if>    order by ba.update_date desc</select>    4)子记录查询
    <select id="selectMaterialCompositions" resultMap="materialCompositionsRigMap">        select           bmc.id "bmc_id"          ,bmc.create_date "bmc_create_date"          ,bmc.update_date "bmc_update_date"        from biz_material_composition bmc       where bmc.artwork_id = #{id}   ### 接收主记录的列名的参数值
    </select>


原文地址:https://www.cnblogs.com/erfsfj-dbc/p/11759784.html

时间: 2024-08-09 09:13:59

使用Mybatis分页插件PageHelper时的分页问题的相关文章

MyBatis学习总结_17_Mybatis分页插件PageHelper

如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该插件目前支持以下数据库的物理分页: Oracle Mysql MariaDB SQLite Hsqldb PostgreSQL DB2 SqlServer(2005,2008) Informix H2 SqlServer2012 配置dialect属性时,可以使用小写形式: Oracle,MySQL

mybatis分页插件PageHelper的使用(转)

Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/HowToUse.markdown 我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 

mybatis分页插件pagehelper

Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina.net/free/Mybatis_PageHelper/blob/master/wikis/HowToUse.markdown 我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 jsqlparser-0.9.4.jar 添加如下依赖: <dep

Mybatis分页插件PageHelper的使用

Mybatis分页插件 - PageHelper支持任何复杂的单表.多表分页.使用方法如下: 1.导入jar包 2.在Maven中添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> 3.在mybatis配置文件中配置插件 <?xml version="1.

MyBatis学习总结(17)——Mybatis分页插件PageHelper

如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该插件目前支持以下数据库的物理分页: Oracle Mysql MariaDB SQLite Hsqldb PostgreSQL DB2 SqlServer(2005,2008) Informix H2 SqlServer2012 配置dialect属性时,可以使用小写形式: oracle,mysql

Mybatis分页插件PageHelper简单使用

1. 引入分页插件 引入分页插件有下面2种方式,推荐使用 Maven 方式. 1). 引入 Jar 包 你可以从下面的地址中下载最新版本的 jar 包 https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/ http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/ 由于使用了sql 解析工具,你还需要下载

基于Mybatis分页插件PageHelper

基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 --><dependency>    <groupId>com.github.pagehelper</groupId>    <artifactId>pagehelper</artifactId>    <version>4.0.

Mybatis分页插件PageHelper的配置和使用方法

http://www.cnblogs.com/kangoroo/p/7998433.html 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分页 一次性请求数据表格中的所有记录(ajax),然后在前端缓存并且计算count和分页逻辑,一般前端组件(例如dataTable)会提供分页动作. 特点是:简单,很适合小规模的web平台:当数据量大的时候会产生性能问题,在查询和网络传输的时间会很长. 后端分页 在aj

springboot +mybatis分页插件PageHelper

1.问题描述 JAVA界ORM的两位大佬Hibernate和Mybatis,hb自带分页(上手挺快,以前用了好几年hb,后期运维及优化快疯了),mybatis没有分页功能,需要借助第三方插件来完成,比较流行的三方框架:PageHelper,今天结合springboot做下介绍,直接贴线上配置,保证可用(如有遗漏,朋友们可以指正下). 2. 解决方案 2.1 配置项目pom.xml <!--分页--> <dependency> <groupId>com.github.pa