一般来说使用 PageHelper 能解决绝大多数的分页问题,相关使用可在博客园上搜索,能找到很多资料。

  之前我在做SpringBoot 项目时遇到这样一个问题,就是当一对多联合查询时需要分页的情况下,使用 PageHelper 做不到对一来进行分页,而是对查询结果做的分页。

  后来经过查找相关资料,找到了一个使用 PageHelper 根据一来进行分页的一对多联合查询,方法就是嵌套子查询,这样的话分页结果就是需要的效果。

  特此将相关代码记录一下,备忘。

  这里使用常见的例子 商品与商品信息 ,在MySql数据库建立两张相对应的表,

CREATE TABLE `item` (
`item_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品编号',
`img_url` varchar(500) NOT NULL DEFAULT '' COMMENT '图片地址',
`title` varchar(1000) NOT NULL COMMENT '标题',
`price` varchar(500) NOT NULL COMMENT '价格',
`item_type` varchar(30) NOT NULL COMMENT '类别',
`quantity` bigint(20) NOT NULL COMMENT '数量',
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='商品';
CREATE TABLE `item_sku` (
`sku_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '规格ID',
`item_id` varchar(30) NOT NULL COMMENT '商品ID',
`sku_price` varchar(100) NOT NULL DEFAULT '' COMMENT 'SKU价格',
`sku_unique_code` varchar(100) NOT NULL COMMENT '规格唯一标识',
`quantity` bigint(20) NOT NULL COMMENT '数量',
PRIMARY KEY (`sku_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='商品SKU';

  在项目中新建相关的model类和mapper接口与xml文件,在xml文件中添加 resultMap

下方的property字段对应关系说明 <!--property字段对应的itemSkus必须在结果集中List的字段名 如:private List<ItemSku> itemSkus;-->

    <resultMap id="item" type="com.demo.dal.entity.pojo.Item">
<result column="item_id" jdbcType="VARCHAR" property="itemId"/>
<result column="img_url" jdbcType="VARCHAR" property="imgUrl"/>
<result column="title" jdbcType="VARCHAR" property="title"/>
<result column="price" jdbcType="VARCHAR" property="price"/>
<result column="item_type" jdbcType="VARCHAR" property="itemType"/>
<result column="quantity" jdbcType="BIGINT" property="quantity"/> <collection property="itemSkus" ofType="com.demo.dal.entity.pojo.ItemSku"
javaType="java.util.List" select="getSkuByItemId"
column="{itemId=item_Id}"><!--{itemId=item_Id,quantity=quantity} 要查询的列 必须在父查询的select字段中--> <!--property字段对应的itemSkus必须在结果集中List的字段名 如:private List<ItemSku> itemSkus;-->
<result column="sku_id" jdbcType="VARCHAR" property="skuId"/>
<result column="sku_price" jdbcType="VARCHAR" property="skuPrice"/>
<result column="sku_unique_code" jdbcType="VARCHAR" property="skuUniqueCode"/>
<result column="quantity" jdbcType="BIGINT" property="quantity"/>
</collection>
</resultMap>

  主查询语句 selectItemAndSku

<select id="selectItemAndSku" resultMap="item" parameterType="map">
SELECT
s1.item_id,
s1.title,
s1.img_url,
s1.item_type,
s1.price,
s1.quantity,
FROM
item s1
<where>
<if test="title != null and title != ''">
AND s1.title LIKE '%${title}%'
</if>
<if test="itemId != null and itemId != ''">
AND s1.item_id = '${itemId}'
</if>
</where>
order by item_id desc
</select>

  子嵌套查询语句 getSkuByItemId

<select id="getSkuByItemId" parameterType="map"
resultType="map">
select s2.sku_id,
s2.sku_price,
s2.sku_unique_code,
s2.quantity,
from item_sku s2
where
s2.item_id = #{itemId}
ORDER BY s2.sku_id
</select>

  当这些都完成后,在mapper接口文件中新添加这样一个方法:  List<Item> selectItemAndSku(Map<String, Object> map);

  然后在service实现类中就可以注入mapper接口类,然后使用分页插件来进行分页了。

示例代码:

  PageInfo<Item> pageInfo =
PageHelper.startPage(page, pageSize)
.doSelectPageInfo(
() ->
itemDao.selectItemAndSku(map);//JDK 8.0以上的语法 //List<Item> list = PageHelper.startPage(page, pageSize);
//itemDao.selectItemAndSku(map);
//PageInfo<Item> pageInfo = new PageInfo<>(list); //通用写法
pageInfo 就是分页出来的结果。通过controller返回出去看看结果✿

SpringBoot Mybatis解决使用PageHelper一对多分页问题的更多相关文章

  1. Mybatis中使用PageHelper插件进行分页

    分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...

  2. spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页

    controller: /**  * 分页查询用户  * @param request  * @param response  * @return  * @throws Exception  */ @ ...

  3. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  4. SpringBoot+Mybatis+PageHelper实现分页

    SpringBoot+Mybatis+PageHelper实现分页 mybatis自己没有分页功能,我们可以通过PageHelper工具来实现分页,非常简单方便 第一步:添加依赖 <depend ...

  5. spring-boot + mybatis +pagehelper 使用分页

    转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...

  6. springboot结合mybatis使用pageHelper插件进行分页查询

    1.pom相关依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  7. springboot mybatis pagehelper 分页问题

    1:添加依赖 compile group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2 ...

  8. springboot+mybatis+pagehelper

    springboot+mybatis+pagehelper整合 springboot   版本2.1.2.RELEASE mybatis  版本3.5 pagehelper 版本5.18 支持在map ...

  9. springboot + mybatis配置分页插件

    一:使用pagehelper配置分页插件 1:首先配置springboot +mybatis框架  参考:http://www.cnblogs.com/liyafei/p/7911549.html 2 ...

随机推荐

  1. LeetCode第2题

    // 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.//// 如果,我们将这两个数相加起来,则会返回一个新的链表 ...

  2. CF356E - Xenia and String Problem

    也许更好的阅读体验 \(\mathcal{Description}\) 定义一种字符串\(gray\)串满足: 长度为奇数 正中间的字母只出现一次 左右两端相同,左右两端也是gray串 一个\(gra ...

  3. 《深入理解 Java 内存模型》读书笔记

    ![img](https://mmbiz.qpic.cn/mmbiz_jpg/1flHOHZw6RtPu3BNx3zps1JhSmPICRw7QgeOmxOfTbCT3RLgIo4qRpn6xL4qg ...

  4. InstantiationException:mybatis.spring.transaction.SpringManagedTransactionFactory

    问题表现 Error creating bean with name 'sqlSessionFactory' Invocation of init method failed; nested exce ...

  5. [leetcode] 234. Palindrome Linked List (easy)

    原题 回文 水题 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * ...

  6. 三、SQL server 2008数据库的备份与还原

    一.SQL数据库的备份:   1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studio → 数据库:Dsi ...

  7. 《C# 语言学习笔记》——事件

    对象可以激活事件,作为它们处理的一部分.给代码添加事件处理程序,这是一种特殊的函数,在事件发生时调用.还可以配置这个处理程序,以监听我们感兴趣的事件. 使用事件可以创建事件驱动的应用程序.许多基于Wi ...

  8. java - 进程和线程的区别及联系

    1. 进程 (1)进程主要有两个特征: a. 进程是一个实体,占有一定的地址空间.每一个进程都有它自己的地址空间,一般情况下,包括文本区域(text region).数据区域(data region) ...

  9. jsp数据交互(二).3

    01.Application原理与应用 01.application对象的作用域范围是整个应用服务,而它在应用中所承担的责任就类似于一个全局变量.只要服务启动,则application对象就会存在. ...

  10. JavaOOP 第二章继承

    一 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具 ...