Mybatis自定义resultMap MyBatis返回数据和数据库查询不一致

本文详细解释了Mybatis中resultMap的去重机制,特别是如何使用discriminator标签来确保查询结果的准确性。讨论了通过添加主键来避免重复数据的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

定义了一个resultMap,如下:

<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是3,但是查询出来的实际数据是2。

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"的标签来设置区分不同结果的标识。

具体步骤如下:

  1. 在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>

  1. 在各个子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就会将它们视为同一条数据,只返回其中的一条。通过这个机制,我们可以有效地去除重复数据,使查询结果更加准确和具有可读性。

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

具体步骤如下:

  1. 在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>

  1. 在各个子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就会将它们视为同一条数据,只返回其中的一条。通过这个机制,我们可以有效地去除重复数据,使查询结果更加准确和具有可读性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值