mybatisplus 自定义mapper加多表联查结合分页插件查询时出现缺失数据的问题

本文主要是介绍mybatisplus 自定义mapper加多表联查结合分页插件查询时出现缺失数据的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题描述

最近做项目时使用了mybatisplus,分页插件也使用的是mybatisplus自带的分页插件,业务需求是查询客户列表,每个客户列表中有一个子列表,在通过分页插件查询后,会出现数量总数为子列表总数、客户列表与子列表不对等。

1、配置mybatis-plus插件

@Configuration
@MapperScan("com.guigu.mapper") //可以将启动类中的注解移到此处
public class MybatisPlusConfig {/**** 1 怎么来配置mybatis-plus中的插件?*   这里所需要的类型是MybatisPlusInterceptor,这是mybatis-plus的一个拦截器,用于配置mybatis-plus中的插件。* 2 为什么要使用拦截器MybatisPlusInterceptor呢?*    这里边的原理和mybatis分页插件的功能是一样的,工作流程如下 :*   (1)第一步:执行查询功能。*   (2)第二步:拦截器对查询功能进行拦截。*   (3)第三步:拦截器对查询功能的基础上做了额外的处理,达到分页的效果(功能)。* 3 对比配置mybatis中的插件?*   用的也是拦截器的方式。** @return MybatisPlusInterceptor*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//添加:分页插件//参数:new PaginationInnerInterceptor(DbType.MYSQL),是专门为mysql定制实现的内部的分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}

2、mapper语句

 需求:根据年龄查询用户列表,分页显示** 第一步:xml自定义分页,Mapper接口方法*1步:如果想要mybatis-plus的分布插件来作用于我们自定义的sql语句的话,* 		第一个参数必须得是一个分页对象:@Param("page") Page<User> page。* 第二步:因为Mapper接口方法有2个参数的话*       方案1:使用mybatis提供的访问方式*       方案2:也可以使用@param来设置命名参数,来规定参数的访问规则Page<CustomerEntity> queryCustomerList(@Param("page") Page<CustomerEntity> customerPage,@Param("customerName") String customerName,@Param("deptIds") List<Long> deptIds,@Param("userIds") List<Long> userIds,@Param("delFlag") Integer logicNotDeleteValue)
    <resultMap id="customerList" type="customerEntity"><id property="customerId" column="customer_id"/><result property="customerName" column="customer_name"/><collection property="children" select="queryList" column="customer_id" ofType="customerInfoEntity"></collection><collection property="children" ofType="customerInfoEntity"><id property="customerInfoId" column="customer_info_id"/><result property="customerId" column="customer_id"/><result property="deptId" column="dept_id"/><result property="industry" column="industry"/><result property="level" column="level"/><result property="source" column="source"/><result property="highSeas" column="high_seas"/><result property="remark" column="remark"/><result property="crtTime" column="crt_time"/><result property="crtName" column="crt_name"/><result property="crtUserId" column="crt_user_id"/></collection></resultMap><select id="queryCustomerList" resultMap="customerList">SELECTc.customer_id,c.customer_name,ci.customer_info_id,ci.customer_id,ci.dept_id,ci.industry,ci.level,ci.source,ci.high_seas,ci.remark,ci.crt_time,ci.crt_name,ci.crt_user_idFROM customer cJOIN customer_info ci on c.customer_id = ci.customer_idwhere ci.high_seas = 0and ci.del_flag = #{delFlag}and c.del_flag =#{delFlag}<if test="customerName != null">AND c.customer_name like concat('%',#{customerName},'%')</if><if test="deptIds.size>0">AND ci.dept_id in<foreach collection="deptIds" item="id" separator="," open="(" close=")">#{id}</foreach></if><if test="userIds.size>0">AND ci.crt_user_id in<foreach collection="userIds" item="id" separator="," open="(" close=")">#{id}</foreach></if></select>

上述语句查询的结果数是表customer_info的数量而不是表customer的数量,客户列表下有多个子列表时会分成多个相同的客户。

解决办法

那我们怎么才能查到正确得一对多的分页结果呢?mybatis提供另一种方式,使用mybatis的子查询映射。

	<resultMap id="customerList" type="customerEntity"><id property="customerId" column="customer_id"/><result property="customerName" column="customer_name"/><collection property="children" select="queryList" column="customer_id" ofType="customerInfoEntity"></collection></resultMap><select id="queryList" resultType="customerInfoEntity">select ci.customer_info_id,ci.customer_id,ci.dept_id,ci.industry,ci.level,ci.source,ci.high_seas,ci.remark,ci.crt_time,ci.crt_name,ci.crt_user_idfrom customer_info ciwhere ci.customer_id = #{customer_id};</select><!--    查询未加入公海池的客户列表--><select id="queryCustomerList" resultMap="customerList">SELECTc.customer_id,c.customer_name,ci.customer_info_id,ci.customer_id,ci.dept_id,ci.industry,ci.level,ci.source,ci.high_seas,ci.remark,ci.crt_time,ci.crt_name,ci.crt_user_idFROM customer cJOIN customer_info ci on c.customer_id = ci.customer_idwhere ci.high_seas = 0and ci.del_flag = #{delFlag}and c.del_flag =#{delFlag}<if test="customerName != null">AND c.customer_name like concat('%',#{customerName},'%')</if><if test="deptIds.size>0">AND ci.dept_id in<foreach collection="deptIds" item="id" separator="," open="(" close=")">#{id}</foreach></if><if test="userIds.size>0">AND ci.crt_user_id in<foreach collection="userIds" item="id" separator="," open="(" close=")">#{id}</foreach></if></select>

注意: collection中的colum属性需要填两表关联的字段,也就是customer_id

这篇关于mybatisplus 自定义mapper加多表联查结合分页插件查询时出现缺失数据的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/227776

相关文章

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll