问题描述

<resultMap id="userVO" type="com.xx.UserVO">
    <result column="userInfo1" jdbcType="VARCHAR" property="userInfo1" />
    <result column="userInfo2" jdbcType="VARCHAR" property="userInfo2" />
    <result column="userInfo3" jdbcType="VARCHAR" property="userInfo3" />
    <result column="cardNo" jdbcType="VARCHAR" property="cardNo" />
    <association property="userDetailVO" javaType="com.xx.UserDetailVO" resultMap="BaseResultMap"/>
</resultMap>
  • 查询出来的 total 是 4,但是程序查询出来的实际数据是 3

原因分析

SQL 查询出来的数据
userInfo1 userInfo2 userInfo3 userDetailVO
1 2 3 {4,5}
1 2 4 {5,6}
1 2 3 {6,7}

Mybatis 处理完之后的数据
userInfo1 userInfo2 userInfo3 userDetailVO
1 2 4 {5,6}
1 2 3 {6,7}

可以知道,我这里是查询一个用户信息 + 用户详情组合。但是 resultInfo1-3 可能会有重复,但是详情是各有不同的,于是 mybatis 就认定相同 resultInfo1-3 相同的是一条数据

于是原本应该是 total = 3 的,实际上查出来的数据只有两条,被 mybatis 做了去重处理

解决方案

  • 在 UserVO 里面加上一个唯一确定的主键,那么这个时候就不存在重复
  • 在 UserVO 里面加上一个能与现在字段构成联合主键的字段,这个时候也不会存在重复,被过滤的情况

原来 Mybatis 的 resultMap 有一个去重机制,它可以帮助我们在查询结果中去除重复的数据。这个机制的核心是使用了一个叫做"discriminator"的标签来设置区分不同结果的标识

案例 DEMO

在 resultMap 中添加 discriminator 标签,设置该标签的 column 属性和 javaType 属性,用于确定区分不同结果的标识是哪个字段,并且该字段的数据类型是什么

<resultMap id="userResultMap" type="User">
  <id property="id" column="id" />
  <result property="username" column="username" />
  <result property="email" column="email" />
  
  <discriminator column="type" javaType="string">
    <case value="1" resultMap="studentResultMap" />
    <case value="2" resultMap="teacherResultMap" />
  </discriminator>
</resultMap>

在各个子 resultMap 中设置唯一区分标识的值,并且保证不同的 resultMap 区分的标识值不同

<resultMap id="studentResultMap" type="Student">
  <id property="id" column="id" />
  <result property="studentName" column="student_name" />
  <result property="studentAge" column="student_age" />
  ...
</resultMap>
 
<resultMap id="teacherResultMap" type="Teacher">
  <id property="id" column="id" />
  <result property="teacherName" column="teacher_name" />
  <result property="teacherAge" column="teacher_age" />
  ...
</resultMap>

这样,当查询结果中有相同区分标识的数据时,Mybatis 就会将它们视为同一条数据,只返回其中的一条。通过这个机制,我们可以有效地去除重复数据,使查询结果更加准确和具有可读性

11-24 21:28