记录Mybatis多层嵌套查询遇到的一个坑
2019-06-14 本文已影响0人
也许________
问题的起因是用到了Mybatis提供的高级查询功能 - 嵌套查询
功能描述:
查询一个用户的基本信息,数据结构组成如下,用户信息-角色信息-权限信息
一个用户拥有多个角色(1对多),一个角色拥有多个权限(1对多)
需要通过一条SQL查询出用户的基本信息还有角色信息以及权限信息
问题描述:
通过如下SQL以及映射无法查询到权限数据
@Alias("users")
public class Users implements Serializable {
private static final long serialVersionUID = 8281654523591594805L;
private int id;
private String userName;
private Date birthday;
private String sex;
private String address;
private String passWord;
List<Role> roleList;
--------
}
@Alias("role")
public class Role implements Serializable {
private static final long serialVersionUID = 1811686676584921517L;
private int id;
private String roleName;
private List<Privilege> privilegeList;
-----------
}
@Alias("privilege")
public class Privilege implements Serializable {
private static final long serialVersionUID = -5886204003263304093L;
private int id;
private String privilegeName;
------------
}
存在问题的映射
通过下面的映射关系无法查询出角色对应的权限集合,问题的原因就在映射结果集中使用了columnPrefix这个属性,该属性的作用是过滤别名前缀
注意:该属性在只有一层嵌套查询时是没问题的,但是多个嵌套查询就有问题了
<resultMap id="selectUserInfoResultMap4" type="users" extends="baseResultMap">
<collection property="roleList" ofType="role" columnPrefix="role_">
<id column="rid" property="id" />
<result column="roleName" property="roleName" />
<collection property="privilegeList" ofType="privilege" columnPrefix="p_">
<id column="pid" property="id" />
<result column="privilegeName" property="privilegeName" />
</collection>
</collection>
</resultMap>
<select id="selectUserInfo" parameterType="Integer" resultMap="selectUserInfoResultMap4">
SELECT
U.ID, U.USERNAME, U.BIRTHDAY, U.SEX, U.ADDRESS, U.PASSWORD,
R.ID role_rid, R.ROLE_NAME role_roleName,
P.ID p_pid, P.PRIVILEGE_NAME p_privilegeName
FROM
USERS U
LEFT JOIN USER_ROLE UR ON U.ID = UR.USER_ID
LEFT JOIN ROLE R ON UR.ROLE_ID = R.ID
LEFT JOIN ROLE_PRIVILEGE RP ON RP.ROLE_ID = R.ID
LEFT JOIN PRIVILEGE P ON P.ID = RP.PRIVILEGE_ID
WHERE
U.ID = #{uid}
</select>
修改后的SQL以及映射
注意映射中去掉了columnPrefix属性,使用完整的别名进行映射,这样就可以查询到角色对应的权限集合数据
<resultMap id="selectUserInfoResultMap3" type="users" extends="baseResultMap">
<collection property="roleList" ofType="role">
<id column="rid" property="id" />
<result column="roleName" property="roleName" />
<collection property="privilegeList" ofType="privilege">
<id column="pid" property="id" />
<result column="privilegeName" property="privilegeName" />
</collection>
</collection>
</resultMap>
<select id="selectUserInfo" parameterType="Integer" resultMap="selectUserInfoResultMap3">
SELECT
U.ID, U.USERNAME, U.BIRTHDAY, U.SEX, U.ADDRESS, U.PASSWORD,
R.ID rid, R.ROLE_NAME roleName,
P.ID pid, P.PRIVILEGE_NAME privilegeName
FROM
USERS U
LEFT JOIN USER_ROLE UR ON U.ID = UR.USER_ID
LEFT JOIN ROLE R ON UR.ROLE_ID = R.ID
LEFT JOIN ROLE_PRIVILEGE RP ON RP.ROLE_ID = R.ID
LEFT JOIN PRIVILEGE P ON P.ID = RP.PRIVILEGE_ID
WHERE
U.ID = #{uid}
</select>