myBatis的一对多查询,主要利用resultMap实现一次查询多个结果集

时间:2022-02-04 08:03:55

日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单。除了常见的关联查询之外,更使用的应该是利用myBatis的resultMap来实现一次查询出多个结果集,缺点:每次组装结果集实际上是重新调用一次连接池,查询大量的数据时会造成资源浪费和效率不高。

首先声明一个BaseresultMapDetail,对应的映射实体类为SysMember,整个resultMap类似这样:

<resultMap id="BaseResultMapDetail" type="com.wx.web.entity.sys.SysMember" >
<id column="id" property="id"/>
<result column="mem_name" property="memName"/>
<result column="mem_pwd" property="memPwd"/>
<result column="mem_qq" property="memQq"/>
<result column="mem_mobile" property="memMobile"/>
<result column="mem_email" property="memEmail"/>
<result column="mem_trueName" property="memTrueName"/>
<result column="mem_status" property="memStatus"/>
<result column="mem_validate_type" property="memValidateType"/>
<result column="mem_type" property="memType"/>
<result column="mem_invitation_code" property="memInvitationCode"/>
<result column="mem_invited_code" property="memInvitedCode"/>
<result column="mem_parent_id" property="memParentId"/>
<result column="item_id" property="itemId"/>
<result column="item_original" property="itemOriginal"/>
<result column="create_by" property="createBy"/>
<result column="create_date" property="createDate"/>
<result column="update_by" property="updateBy"/>
<result column="update_date" property="updateDate"/>
<result column="remarks" property="remarks"/>
<result column="del_flag" property="delFlag"/>
<association property="parentMember" column="mem_parent_id" select="selectParentMemberById"></association>
<collection property="roleList" column="id" ofType="com.wx.web.entity.sys.SysRole" select="selectRoleList"></collection>
<collection property="menuList" column="id" ofType="com.wx.web.entity.sys.SysMenu" select="selectMenuList"></collection>
<collection property="menuBtnList" column="id" ofType="com.wx.web.entity.sys.SysMenuBtn" select="selectMenuBtnList"></collection>

</resultMap>

如上所示,查询一条记录的是使用association,查询数据集合List的时候使用collection,property对应实体类中的属性,column为所对应的外键字段名称,ofType对应映射的实体类,select为另一个查询(一个单独的查询结果),形式如下:

<!-- 根据id查询角色列表 -->
<select id="selectParentMemberById" resultType="com.wx.web.entity.sys.SysMember" parameterType="java.lang.String">
SELECT
t.id AS id,
t.mem_name AS memName,
t.mem_mobile AS memMobile,
t.mem_type AS memType,
t.mem_invitation_code AS memInvitationCode,
t.mem_trueName AS memTrueName
FROM
tb_sys_member t
WHERE
t.id = #{id} and t.del_flag=0
</select> <!-- 根据id查询角色列表 -->
<select id="selectRoleList" resultType="com.wx.web.entity.sys.SysRole" parameterType="java.lang.String">
SELECT
t.id AS id,
t.role_code AS roleCode,
t.role_name AS roleName
FROM
tb_sys_role t
LEFT JOIN tb_sys_role_member t1 ON t.id = t1.role_id
WHERE
t1.member_id = #{id} and t.del_flag=0
</select> <!-- 根据id查询菜单列表 -->
<select id="selectMenuList" resultType="com.wx.web.entity.sys.SysMenu" parameterType="java.lang.String">
SELECT DISTINCT
t.id AS id,
t.menu_grade AS menuGrade,
t.menu_name AS menuName,
t.menu_url as menuUrl,
t.parent_menu_id as 'parentMenu.id',
t.create_date as createDate
FROM
tb_sys_menu t
LEFT JOIN tb_sys_role_menu t1 ON t.id = t1.menu_id
LEFT JOIN tb_sys_role_member t2 on t1.role_id = t2.role_id
WHERE
t2.member_id = #{id} and t.del_flag=0
order by t.create_date
</select> <!-- 根据id查询菜单按钮列表 -->
<select id="selectMenuBtnList" resultType="com.wx.web.entity.sys.SysMenuBtn" parameterType="java.lang.String">
SELECT DISTINCT
t.id AS id,
t.menu_id AS menuId,
t.btn_name AS btnName,
t.btn_permissions AS btnPermissions
FROM
tb_sys_menu_btn t
LEFT JOIN tb_sys_role_menu t1 ON t.id = t1.menu_id
LEFT JOIN tb_sys_role_member t2 on t1.role_id = t2.role_id
WHERE
t2.member_id = #{id} and t.del_flag=0
order by t.create_date
</select>

并且SysMember实体类中定义好相应的属性,并实现get/set方法:

private SysMember parentMember; // 上级用户实体类
private List<SysRole> roleList; // 拥有的角色
private List<SysMenu> menuList; // 拥有的菜单
private List<String> permissionsList; // 拥有的菜单权限
private List<SysMenuBtn> menuBtnList; // 拥有的菜单按钮

具体查询使用的时候,指定resultMap为事先声明的BaseresultMapDetail,

<!-- 根据id查询 会员 -->
<select id="queryById" resultMap="BaseResultMapDetail" parameterType="Object">
select <include refid="Base_Column_List" />
from tb_sys_member
where id = #{id} and del_flag=0
</select>

当执行queryById的时候,debug调试时可以发现除了执行queryById本身的sql之外,还执行了四次sql,分是selectParentMemberById、selectRoleList、selectMenuList、selectMenuBtnList对应的查询操作。执行后将这些查询结果全部映射到SysMember中的各个实体类属性。