Ibatis中返回一对多结果集的解决方法

之前遇到过类似的问题,今天记录下。版本比较低的文档中(具体几的文档忘记了..)记得有介绍在<resultMap>下<result>标签使用 select属性。例如:

<resultMap class="xxx" id="xx">
    <result property="xxx" column="xxx" select="xxSQL" />
</resultMap>

但是这种解决方法不是很好。如果在Bean中的属性有集合类,可以使用以下的方式

<sqlMap namespace="xxxSQL">
    <typeAlias alias="sysUser"
        type="xx.SysUser" />
    <typeAlias alias="userCommunity"
        type="xx.UserCommunity" />
    
    <resultMap class="userCommunity" id="comm" >
        <result property="communityCode" column="COMMUNITY_CODE"/>
        <result property="communityName" column="MC" />
    </resultMap>
    <resultMap class="sysUser" id="sysCom" groupBy="loginId">
        <result property="loginId" column="LOGIN_ID"/>
        <result property="userCommunityList" resultMap="xxxSQL.comm"/>
    </resultMap>
    
    <select id="getSysUserRole" resultMap="sysCom">
        SELECT S.LOGIN_ID, U.COMMUNITY_CODE, C.MC
         FROM SYS_USER S, USER_COMMUNITY U, ccc C
        WHERE S.LOGIN_ID = U.POLICE_NO
        AND u.community_code = c.dm
        ORDER BY S.LOGIN_ID 
    </select>
</sqlMap>    

需要注意的是,在id='sysCom'的<result property="userCommunityList" resultMap="xxxSQL.comm" />中一定要指定SQL的namespace。

-------------------------------------分割线----------未完待补充---------------------

原文地址:https://www.cnblogs.com/GYoungBean/p/2814290.html