问题:
定义了一个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"的标签来设置区分不同结果的标识。
具体步骤如下:
- 在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就会将它们视为同一条数据,只返回其中的一条。通过这个机制,我们可以有效地去除重复数据,使查询结果更加准确和具有可读性。
Mybatis的resultMap有一个去重机制,它可以帮助我们在查询结果中去除重复的数据。这个机制的核心是使用了一个叫做"discriminator"的标签来设置区分不同结果的标识。
具体步骤如下:
- 在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就会将它们视为同一条数据,只返回其中的一条。通过这个机制,我们可以有效地去除重复数据,使查询结果更加准确和具有可读性。