resultMap中的collection集合出现只能读取一条数据的解决方法

查询数据时只能获得collection集合中的的一条数据,相关情况如下:

结果集resultMap:

<resultMap id="ManagerRolesAcls" type="com.meikai.shop.entity.TSystemManager">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
<result column="manager_name" jdbcType="VARCHAR" property="managerName" />
<result column="nickname" jdbcType="VARCHAR" property="nickname" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="avatar" jdbcType="VARCHAR" property="avatar" />
<result column="is_enable" jdbcType="INTEGER" property="isEnable" />
<result column="is_locked" jdbcType="INTEGER" property="isLocked" />
<result column="try_num" jdbcType="INTEGER" property="tryNum" />
<result column="locked_date" jdbcType="TIMESTAMP" property="lockedDate" />
<result column="is_forver" jdbcType="INTEGER" property="isForver" />
<result column="login_time" jdbcType="TIMESTAMP" property="loginTime" />
<result column="login_ip" jdbcType="VARCHAR" property="loginIp" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="active_code" jdbcType="VARCHAR" property="activeCode" />
<!-- 拥有角色集合 -->
<collection property="roles" ofType="com.meikai.shop.entity.TSystemRole" javaType="java.util.ArrayList">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="role_name" jdbcType="VARCHAR" property="roleName" />
<!-- 拥有资源集合 -->
<collection property="acls" ofType="com.meikai.shop.entity.TSystemAcl" javaType="java.util.ArrayList">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="permission" jdbcType="VARCHAR" property="permission" />
</collection>
</collection> 
</resultMap>

5表关联查询:

<!-- 根据用户ID获得用户及其角色和资源 -->
<select id="getRolesAndAclsByID" parameterType="Long" resultMap="ManagerRolesAcls">
SELECT m.*,r.ID,r.role_name,a.ID,a.permission
FROM t_system_manager m 
LEFT JOIN 
t_system_manager_role mr ON m.ID=mr.manager_id 
LEFT JOIN t_system_role r ON mr.`role_id`=r.`ID` 
LEFT JOIN t_system_role_acl ra ON r.`ID`=ra.`role_id` 
LEFT JOIN t_system_acl a ON a.`ID`=ra.`acl_id` 
WHERE m.ID=#{id,jdbcType=BIGINT}; 
</select>

根据ID获得管理员:


@RequestMapping(value= {"view"},method = { RequestMethod.GET })
@ResponseBody
public TSystemManager view(HttpServletRequest request,Long id) {

TSystemManager viewManager = managerService.getRolesAndAclsByID(id);

    for(TSystemRole role : viewManager.getRoles()) {
           System.out.append("拥有的角色是:"+role.getRoleName());

                for(TSystemAcl acl : role.getAcls()) {
                     System.out.append("拥有的资源是:"+acl.getPermission());
                }
    }
   return viewManager;
}

在mysql通过上述5表查询语句能查询多条关于角色和资源的记录,但是通过mybatis查询获得的只有单条角色和资源的数据。

在sqlyog的查询结果

通过mybatis获得的结果

排查许久发现原因是:

1、主表的id字段和从表的id字段一样;

2、collection集合中包含<id column="ID" jdbcType="BIGINT" property="id" />;

如果同时存在上面两种情况,就只能获得collection的一条数据。

解决方法:

去掉collection的<id column="ID" jdbcType="BIGINT" property="id" />后,才能查询出全部数据。

原文地址:https://www.cnblogs.com/kenhome/p/7647132.html