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

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

MYSQL查询结果实现发送给客户端

《MYSQL查询结果实现发送给客户端》:本文主要介绍MYSQL查询结果实现发送给客户端方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql取数据和发数据的流程(边读边发)Sending to clientSending DataLRU(Least Rec

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig