日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单。除了常见的关联查询之外,更使用的应该是利用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中的各个实体类属性。

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

  1. mybatis collection 一对多关联查询,单边分页的问题总结!

    若想直接通过sql实现多级关联查询表结构得有2 个必不可少的字段:id ,parentId,levelId id:主键id, parentId:父id levelId:表示第几级(表本身关联查询的时候 ...

  2. Mybatis学习——一对多关联表查询

    1.实体类 public class Student { private int id; private String name; } public class Classes { private i ...

  3. 19_高级映射:一对多查询(使用resultMap)

    [需求] 查询订单以及订单明细的信息. 确定主查询表:订单表orders 确定关联查询表:订单明细表 orderdetail 在一对一查询的基础上添加订单明细表关联即可. [分析] 使用resultM ...

  4. MyBatis:一对多关联查询

    MyBatis从入门到放弃四:一对多关联查询 前言 上篇学习了一对一关联查询,这篇我们学习一对多关联查询.一对多关联查询关键点则依然是配置resultMap,在resultMap中配置collecti ...

  5. java:Mybatis框架2(基于mapper接口的开发,多种查询,复合类型查询,resultMap定义,多表联查,sql片段)

    1.mybatis02: mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> &l ...

  6. mybatis中一对多查询collection关联不执行

    今天遇到的原因是因为下面红底id没有,导致关联查询没有条件(id字段没传),所以一直没有执行. <?xml version="1.0" encoding="UTF- ...

  7. mybatis 13: 一对多关联查询

    业务背景 根据客户id查询客户基本信息,以及客户存在的订单信息 两张数据表 客户表 订单表 实体类 客户实体类:Customer private Integer id; private String ...

  8. mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条

    mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条

  9. 好947 Mybatis 配置resultMap 带參数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错

    //TMD 写几个demo 还有大站採集 <a target=_blank href="http://hao947.com/" target="_blank&quo ...

随机推荐

  1. nginx error_log 错误日志配置说明

    nginx的error_log类型如下(从左到右:debug最详细 crit最少): [ debug | info | notice | warn | error | crit ] 例如:error_ ...

  2. java2

    1:关键字(掌握) (1)被Java语言赋予特定含义的单词 (2)特点: 全部小写. (3)注意事项: A:goto和const作为保留字存在. B:类似于Notepad++这样的高级记事本会对关键字 ...

  3. ubuntu下安装nodejs,无node情况

      Updating nodejs solved the issue: npm cache clean -f npm install -g n n stable node --version node ...

  4. CoreAnimation 之CAReplicatorLayer

    CAReplicatorLayer: 主要作用有以下两个: CAReplicatorLayer的目的是为了高效生成许多相似的图层,它会绘制一个或多个图层的子图层 并在每个复制体上应用不同的变换 使用C ...

  5. DIV+CSS 清除浮动方法总结

    DIV+CSS 清除浮动是页面布局中常见的问题,相信各位高手也都有自己的方法,今天在这里对常见的几种方法进行总结(PS:谈不上是原创,这里是我自己做的归纳总结,也是我自己内化的过程),希望对您能够有所 ...

  6. LeetCode之383. Ransom Note

    -------------------------------------------- 思路就是进行频率统计. 统计一下第二个字符串字符出现次数++统计一下第一个字符串中字符出现次数--如果出现负数 ...

  7. XML中<beans>中属性概述

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  8. form.submit(回调函数)——引用jq-form.js

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. 教你几种在SQLServer中删除重复数据方法(转)

    转载地址:http://www.jb51.net/article/22980.htm 方法一 复制代码 代码如下: declare @max integer,@id integer declare c ...

  10. [JAVA]HTTP请求应答作输入输出

    请求(需要发送数据给别人): URL url = new URL("需要请求的URL连接"); HttpURLConnection httpConnection = (HttpUR ...