首页 > 其他分享 >解决mybatis嵌套查询使用PageHelper分页不准确

解决mybatis嵌套查询使用PageHelper分页不准确

时间:2023-11-07 13:33:21浏览次数:29  
标签:oi item od PageHelper 嵌套 mybatis order os id

版权声明:本文为CSDN博主「那只小猿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41869361/article/details/116427385

springboot项目中嵌套查询使用PageHelper发现使用结果不正确,上PageHelper官网看了一下果然是有坑。。

 下面这种映射会导致分页结果不正确

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.mapper.OrdersMapperCustom">
<resultMap id="MyOrdersList" type="com.imooc.pojo.vo.MyOrdersVO">
<id column="orderId" property="orderId" />
<result column="createdTime" property="createdTime" />
<result column="payMethod" property="payMethod" />
<result column="realPayAmount" property="realPayAmount" />
<result column="postAmount" property="postAmount" />
<result column="orderStatus" property="orderStatus" />

<!--
collection 标签:用户定义关联的list集合
property 对应属性名
ofType 集合的类型
-->
<collection property="subOrderItemList" ofType="com.imooc.pojo.vo.MySubOrderItemVO">
<result column="itemId" property="itemId" />
<result column="itemImg" property="itemImg" />
<result column="itemName" property="itemName" />
<result column="itemSpecName" property="itemSpecName" />
<result column="buyCounts" property="buyCounts" />
<result column="price" property="price" />
</collection>
</resultMap>

<select id="queryMyOrdersNotUse" resultMap="MyOrdersList" parameterType="Map">
SELECT
od.id AS orderId,
od.created_time AS createdTime,
od.pay_method AS payMethod,
od.real_pay_amount AS realPayAmount,
od.post_amount AS postAmount,
os.order_status AS orderStatus,
oi.item_id AS itemId,
oi.item_name AS itemName,
oi.item_img AS itemImg,
oi.item_spec_name AS itemSpecName,
oi.buy_counts AS byuCounts,
oi.price AS price
FROM
orders od
LEFT JOIN order_items oi ON od.id = oi.order_id
LEFT JOIN order_status os ON od.id = os.order_id
WHERE
od.user_id = #{paramsMap.userId}
AND od.is_delete = 0
<if test="paramsMap.orderStatus != null">
and os.order_status = #{paramsMap.orderStatus}
</if>
ORDER BY
od.updated_time ASC
</select>

正确显示分页的代码:如下,把之前的一条嵌套查询拆分成两个查询,就等同于子查询的效果,
在collection 标签加上select属性 getSubItems 映射下面的sql,column用于传递参数

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.mapper.OrdersMapperCustom">
<resultMap id="MyOrdersList" type="com.imooc.pojo.vo.MyOrdersVO">
<id column="orderId" property="orderId" />
<result column="createdTime" property="createdTime" />
<result column="payMethod" property="payMethod" />
<result column="realPayAmount" property="realPayAmount" />
<result column="postAmount" property="postAmount" />
<result column="orderStatus" property="orderStatus" />

<!--
collection 标签:用户定义关联的list集合
property 对应属性名
ofType 集合的类型
-->
<collection property="subOrderItemList" select="getSubItems" column="orderId" ofType="com.imooc.pojo.vo.MySubOrderItemVO">
<result column="itemId" property="itemId" />
<result column="itemImg" property="itemImg" />
<result column="itemName" property="itemName" />
<result column="itemSpecName" property="itemSpecName" />
<result column="buyCounts" property="buyCounts" />
<result column="price" property="price" />
</collection>
</resultMap>

<select id="queryMyOrders" resultMap="MyOrdersList" parameterType="Map">
SELECT
od.id AS orderId,
od.created_time AS createdTime,
od.pay_method AS payMethod,
od.real_pay_amount AS realPayAmount,
od.post_amount AS postAmount,
os.order_status AS orderStatus
FROM
orders od
LEFT JOIN order_status os ON od.id = os.order_id
WHERE
od.user_id = #{paramsMap.userId}
AND od.is_delete = 0
<if test="paramsMap.orderStatus != null">
and os.order_status = #{paramsMap.orderStatus}
</if>
ORDER BY
od.updated_time ASC
</select>

<select id="getSubItems" parameterType="string" resultType="com.imooc.pojo.vo.MySubOrderItemVO">
select
oi.item_id AS itemId,
oi.item_name AS itemName,
oi.item_img AS itemImg,
oi.item_spec_name AS itemSpecName,
oi.buy_counts AS byuCounts,
oi.price AS price
from
order_items oi
where
oi.order_id = #{orderId}
</select>

 

标签:oi,item,od,PageHelper,嵌套,mybatis,order,os,id
From: https://www.cnblogs.com/andy1234/p/17814785.html

相关文章

  • pageHelper 插件一对多查询分页问题
    1.首先先确定我们需要返回的数据数据结构,这里我的贴出实体类(set/get方法自己生成)publicclassBillInfoAndStudentInfoBean{privateStringid;privateStringbillId;privateBigDecimalmoneyTotal;privateList<ItemsBean>items;}2.然后我们在mapper.xm......
  • springboot mybatis-plus 登录接口
    下面是使用SpringBoot和MyBatis-Plus实现登录接口的示例代码:添加依赖在pom.xml文件中添加以下依赖:<dependencies><!--SpringBoot--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</a......
  • springboot+mybatis-plus批量删除
    可以使用Mybatis-Plus提供的deleteBatchIds方法来实现批量删除。示例代码:@AutowiredprivateMybatisPlusMappermybatisPlusMapper;publicvoiddeleteBatch(List<Long>ids){mybatisPlusMapper.deleteBatchIds(ids);}其中,MybatisPlusMapper是你的Mapper接口,继承了BaseMa......
  • javaweb-- Mybatis参数传递
     Mybatis提供了ParamNameResolver类进行封装 传入多个参数时,mybatis会将参数封装成Map集合map.put("arg0",参数值1)map.put("param1",参数值1)map.put("arg1",参数值2)map.put("param2",参数值2) ......
  • 通过mybatis-plus的自定义拦截器实现控制 mybatis-plus的全局逻辑删除字段的控制 (修改
    需求:过滤部分请求不实现mybatis-plus的逻辑删除看到网上关于mybatis-plus的自定义拦截器的文章有的少想了想自己写了一篇欢迎参考指正通过springboot的拦截器在请求进来时标记需要实现的需求的逻辑importlombok.Data;@DatapublicclassSyncBo{privateBoolean......
  • 【mysql】获取某个表所有列名【mybatis】
    方法1:[仅指定表名]selectCOLUMN_NAMEfrominformation_schema.COLUMNSwheretable_name='your-table-name'; 方法2:[指定表名+数据库名]selectCOLUMN_NAMEfrominformation_schema.COLUMNSwheretable_name='your-table-name'andtable_schema='your-DB......
  • SpringBoot系列之MyBatis Plus自动填充实现
    系列博客专栏:SpringBoot2.0系列博客专栏开发环境JDK1.8SpringBoot2.2.1Maven3.2+Mysql5.7.36开发工具IntelliJIDEAsmartGit项目场景在项目中经常会遇到需要自动填充数据的情况,比如新增一个DO类,里面可能会有id、create_time、modify_time、create_u......
  • MyBatis了解
    MyBatis了解MyBatis是什么?MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(PlainOldJavaObjects,普通的Java对象)映射......
  • Kotlin-嵌套类_内部类_匿名内部类
    Kotlin-嵌套类&内部类&匿名内部类1.嵌套类类可以被嵌套在其它类中:classOuter{privatevalbar:Int=1classNested{funfoo(){println("fooinOuter#Nested#foo()")}funtest(){//println("Ne......
  • Mybatis中 collection 和 association 标签 的区别
    版权声明:本文为CSDN博主「时夏゛」的原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/XikYu/article/details/132761255<collection>和<association>是MyBatis中用于定义映射关系的标签,它们的区别如下:目标对象类型:<colle......