在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ mybatis嵌套查詢報錯TooManyResultsException

mybatis嵌套查詢報錯TooManyResultsException

//com.arnold.rbac.dao.SysRoleMapper#getRoleWithPermission
SysRole getRoleWithPermission(Integer id);
public class SysRole {
    //省略其他
    private List<SysPermission> permission;
}

dao方法報錯

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
<resultMap id="RoleWithPermissionResultMap" type="com.arnold.rbac.model.SysRole">
    <id column="sr_id" jdbcType="INTEGER" property="id"/>
    <result column="role_name" jdbcType="VARCHAR" property="roleName"/>
    <result column="status" jdbcType="VARCHAR" property="status"/>
    <result column="remark" jdbcType="VARCHAR" property="remark"/>
    <result column="creater" jdbcType="VARCHAR" property="creater"/>
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
    <result column="editor" jdbcType="VARCHAR" property="editor"/>
    <result column="editor_time" jdbcType="TIMESTAMP" property="editorTime"/>
    <collection property="permission" javaType="java.util.ArrayList" column="permissionId" ofType="com.arnold.rbac.model.SysPermission" select="com.arnold.rbac.dao.SysPermissionMapper.selectByPrimaryKey">

    </collection>
</resultMap>
    
    
<select id="getRoleWithPermission" resultMap="RoleWithPermissionResultMap">
    select
    sr.id as sr_id, role_name, status, remark, creater, create_time, editor, editor_time,
    srp.permission_id as permissionId
    from sys_role sr
    LEFT JOIN sys_role_permission srp ON srp.role_id = sr.id
    where sr.id = #{id,jdbcType=INTEGER}
</select>
<mapper namespace="com.arnold.rbac.dao.SysPermissionMapper">
    <resultMap id="BaseResultMap" type="com.arnold.rbac.model.SysPermission">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="permission_name" jdbcType="VARCHAR" property="permissionName"/>
        <result column="permission_str" jdbcType="VARCHAR" property="permissionStr"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="is_del" jdbcType="BIT" property="isDel"/>
        <result column="creater" jdbcType="VARCHAR" property="creater"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="editor" jdbcType="VARCHAR" property="editor"/>
        <result column="edit_time" jdbcType="TIMESTAMP" property="editTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, permission_name, permission_str, status, is_del, creater, create_time, editor,
        edit_time
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where id = #{id,jdbcType=INTEGER}
    </select>
</mapper>

如果將dao變?yōu)?br>List<SysRole> getRoleWithPermission(Integer id);
則返回3條記錄,每個role對應一個permission。

回答
編輯回答
祉小皓

因為確實sql語句查出3條記錄
如果使用<collection select="">來查找的話,只能傳入一個值。所以結果只能是一條記錄。
如果使用<collection resultMap> 的話,mybatis會將查出的多條結果按主鍵id合并到collection。

<resultMap id="RoleWithPermissionResultMap" type="com.arnold.rbac.model.SysRole">
        <id column="sr_id" jdbcType="INTEGER" property="id"/>
        <result column="role_name" jdbcType="VARCHAR" property="roleName"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="creater" jdbcType="VARCHAR" property="creater"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="editor" jdbcType="VARCHAR" property="editor"/>
        <result column="editor_time" jdbcType="TIMESTAMP" property="editorTime"/>
        <collection property="permissionIds" javaType="java.util.ArrayList" ofType="java.lang.Integer">
            <result column="permissionId" />
        </collection>
        <collection property="permission" javaType="java.util.ArrayList" columnPrefix="sp_" ofType="com.arnold.rbac.model.SysPermission" resultMap="com.arnold.rbac.dao.SysPermissionMapper.BaseResultMap">
        </collection>
    </resultMap>
2017年2月28日 02:09