天天看点

Mybatis:Mybatis使用<resultmap>映射,查询结果和映射结果不一致

使用的是Mybatis-plus版本是:3.3.2

一、发现问题

    Mybatis使用<resultmap>做映射,详细如下

<resultMap id="userDictionary" type="com.xuexue.firstproject.pojo.bo.RealmDictionaryBo">
    <result property="userName" column="user_name"/>
    <association property="dictionary" javaType="com.xuexue.firstproject.pojo.po.Dictionary">
        <id property="id" column="id"/>
        <result property="dictType" column="dict_type"/>
        <result property="dictName" column="dict_name"/>
    </association>
</resultMap>
           

    查询出的结果一共有4条,但是映射的结果只有3条数据。这里的user_name列有两个名字是重复的。

二、解决问题

    上网查询了资料,得知我的问题出在,通过<association>映射对象,和他同级的<result>的值会相同,导致映射结果和查询结果不一致,自动的将我重复的数据给去掉了。

    我的解决方法就是加上一个唯一标识

<resultMap id="userDictionary" type="com.xuexue.firstproject.pojo.bo.RealmDictionaryBo">
    <id property="id" column="id"/>    
    <result property="userName" column="user_name"/>
    <association property="dictionary" javaType="com.xuexue.firstproject.pojo.po.Dictionary">
        <id property="id" column="id"/>
        <result property="dictType" column="dict_type"/>
        <result property="dictName" column="dict_name"/>
    </association>
</resultMap>