日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单。除了常见的关联查询之外,更使用的应该是利用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. javascript数据结构-数组

    github博客地址 简介 数组是最简单的数据结构,javascript语言也很早就原声支持了数组Array数据类型,和其他语言略微不同的是:js中的数组可以存储不同类型的值,看起来很厉害的样子,但是 ...

  2. Python简单源码解析

    主要为一些简单的源代码的解析以及一些方法的理解. 说明:这些文件都不是我写的,详情可参考Github上的内容. 批量修改文件类型 def batch_rename(work_dir, old_ext, ...

  3. python set

    set是一个工厂函数(filter也是工厂函数),是一个可变的集合 frozenset 不可变的集合,与set共性,他也在内部自动去重, >>> num5=frozenset([1, ...

  4. js 的复制和引用 (传值和传址)

    复制(传值-实参):  基本类型赋值.函数参数 引用(传址-形参):  对象.数组.函数

  5. ios推送-B/S架构-socket

    B/S架构项目,某一用户登录后执行了某些动作需要向在手机登录的对应的用户推送消息 通过socket实现 1.socket服务器:使用C#的window服务(该服务监听两个端口:比如平台8889,手机8 ...

  6. NEFU 560 半数集

    题目链接 递推就行,把a[0]设为1很巧妙. #include <cstdio> #include <iostream> using namespace std; ]={}; ...

  7. iOS8系统H264视频硬件编解码说明

    公司项目原因,接触了一下视频流H264的编解码知识,之前项目使用的是FFMpeg多媒体库,利用CPU做视频的编码和解码,俗称为软编软解.该方法比较通用,但是占用CPU资源,编解码效率不高.一般系统都会 ...

  8. ASP.NET知识总结(1.网络传输层)

    1.网络传输层 1应用层(HTTP.FTP.SMTP)报文Message 2传输层(TCP.UDP)报文段Segment,为运行在不同主机上的应用程序进程间提供数据 传输服务.通过套接字(Socket ...

  9. oracle数据库安装完要做的事情。

    安装数据库客户端.这个在oracle官网可以下载安装(下载链接) 安装PL/SQL PL在下载 配置环境变量(这个比较重要,不配置PLSQL链接不到数据库) 配置的相关环境变量有: 变量名:oracl ...

  10. 解析 Linux 内核可装载模块的版本检查机制

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/ 为保持 Linux 内核的稳定与可持续发展,内核在发展过程中引进了可 ...