SpringBoot Mybatis解决使用PageHelper一对多分页问题
一般来说使用 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返回出去看看结果✿