天天看點

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>