MyBatis高级查询

使用自动映射处理一对一映射

一个用户只能拥有一个角色。

使用自动映射就是通过别名让MyBatis自动将值匹配到相应的字段上,简单的别名映射如:
user_name对应UserName。除此之外Mybatis还支持复杂的属性映射,可以多层嵌套,例如将:
role.role_name映射到role.roleName上。MyBatis会先查找role属性,如果存在role属性就创建role对象,然后在role对象中继续查找roleName,将role_name的值绑定到role的roleName属性上。

下面根据自动映射规则,在UserMapper中增加如下方法。


   <select id="selectUserAndRoleById" resultMap="userRoleMap">
        SELECT
            u.id,
            u.user_name,
            u.user_password,
            u.user_email,
            u.user_info,
            u.head_img,
            u.create_time,
            r.id "role_id",
            r.role_name "role.roleName",
            r.enabled "role.enabled",
            r.create_by "role.createBy",
            r.create_time "role.createTime"
        FROM sys_user u
        INNER JOIN sys_user_role ur on u.id = ur.user_id
        INNER JOIN sys_role r on ur.role_id = r.id
        WHERE u.id = #{id}
    </select>

使用resultMap配置一一映射

先写好resultMap,然后查询语句也与自动映射的不太一样

<resultMap id="userMap" type="pers.congcong.myBatis2.pojos.SysUser">
        <result property="userName" column="user_name"/>
        <result property="userPassword" column="user_password"/>
        <result property="userEmail" column="user_email"/>
        <result property="userInfo" column="user_info"/>
        <result property="headImg" column="head_img" jdbcType="BLOB"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="roleMap" type="pers.congcong.myBatis2.pojos.SysRole">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="role_name" jdbcType="VARCHAR" property="roleName" />
        <result column="enabled" jdbcType="INTEGER" property="enabled" />
        <result column="create_by" jdbcType="BIGINT" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    </resultMap>

    <resultMap id="userRoleMap" extends="userMap" type="pers.congcong.myBatis2.pojos.SysUser">
        <result column="id" jdbcType="BIGINT" property="id" />
        <result column="role_name" jdbcType="VARCHAR" property="roleName" />
        <result column="enabled" jdbcType="INTEGER" property="enabled" />
        <result column="create_by" jdbcType="BIGINT" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    </resultMap>


    <select id="selectUserAndRoleById" resultMap="userRoleMap">
        SELECT
            u.id,
            u.user_name,
            u.user_password,
            u.user_email,
            u.user_info,
            u.head_img,
            u.create_time,
            r.id "role_id",
            r.role_name "role_role_name",
            r.enabled "role_enabled",
            r.create_by "role_create_by",
            r.create_time "role_create_time"
        FROM sys_user u
        INNER JOIN sys_user_role ur on u.id = ur.user_id
        INNER JOIN sys_role r on ur.role_id = r.id
        WHERE u.id = #{id}
    </select>

第三种 使用resultMap的association标签一对一映射

查询语句跟使用resultMap的一样,只不过将resultMap封装了,然后用association标签引用
代码如下:

    <resultMap id="userMap" type="pers.congcong.myBatis2.pojos.SysUser">
        <result property="userName" column="user_name"/>
        <result property="userPassword" column="user_password"/>
        <result property="userEmail" column="user_email"/>
        <result property="userInfo" column="user_info"/>
        <result property="headImg" column="head_img" jdbcType="BLOB"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="roleMap" type="pers.congcong.myBatis2.pojos.SysRole">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="role_name" jdbcType="VARCHAR" property="roleName" />
        <result column="enabled" jdbcType="INTEGER" property="enabled" />
        <result column="create_by" jdbcType="BIGINT" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    </resultMap>

    <resultMap id="userRoleMap" extends="userMap" type="pers.congcong.myBatis2.pojos.SysUser">
        <association property="sysRole" columnPrefix="role_" resultMap="roleMap" />
    </resultMap>

第四种 association标签的嵌套查询:

除了前面3中通过复杂SQL查询获取结果,还可以利用简单SQL通过多次查询转换为我们的结果,这种方式与根据业务的逻辑手动执行多次SQL的方式相像,最后会合成一个对象。

association标签的嵌套查询
实则上是将select封装好,然后通过association调用。
association标签的嵌套查询常用的属性如下:
select:另一个映射查询的ID,MyBatis会额外执行这个查询获取这个对象的结果。
column:列名,将主查询中列的结果作为嵌套查询的参数,配置方法如:
column={propl=coll, prop2=coll2},prop1和prop2将作为嵌套查询的参数。
fetchType:数据的加载方式,可选值为lazy和eager,分别为延迟加载和积极加载,这个配置会覆盖全局的lazyLodingEnable配置。

看得差不多,不抄书了,上代码:
····

<resultMap id="userRoleMapSelect" type="pers.congcong.myBatis2.pojos.SysUser" extends="userMap">
    <association property="sysRole" fetchType="lazy" column="{id = role_id}"         select="pers.congcong.myBatis2.mappers.RoleMapper.selectById"/>
</resultMap>

<select id="selectUserAndRoleByIdSelect" resultMap="userRoleMapSelect">
    SELECT
        u.id,
        u.user_name,
        u.user_password,
        u.user_email,
        u.user_info,
        u.head_img,
        u.create_time,
        ur.role_id
    FROM sys_user u
    INNER JOIN sys_user_role ur on u.id = ur.user_id
    WHERE u.id = #{id}
</select>

····

测试代码:

   @Test
    public void testSelectUserAndRoleById() {
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            SysUser sysUser = userMapper.selectUserAndRoleByIdSelect(1001l);

            System.out.println("调用user.getSysRole()");

            Assert.assertNotNull(sysUser.getSysRole());
        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }
    }

结果:


fetchType="lazy"

这里使用fetchType为lazy,要配置这个属性的时候要修改mybatis-config.xml
添加

 <setting name="aggressiveLazyLoading" value="false"/>

就可以了。

一对多映射

collection集合的嵌套结果映射:

和association类似,集合的嵌套结果就是通过一次SQL查询将所有的结果查询出来,然后通过配置的结果映射,将数据映射到不同的对象中去。在一对多的关系中,主表的一条数据会对应关联表中的多条数据,因此,一般查询时会查询出多个结果,按照一对多的数据结果存储数据的时候,最终的结果会小于查询的总记录数。

需求:
一个用户拥有多个角色,每个角色又拥有多个权限。所以要渐进式的实现一个SQL,查询出所有用户和用户拥有的角色,以及角色所包含的所有权限信息的两层嵌套结果。

解决:
首先在Role中添加角色的集合:

private List<SysRole> sysRoleList;
以及setter,getter方法。

然后在UserMapper.xml中创建resultMap,select方法,代码如下:

    <resultMap id="userRoleListMap" extends="userMap" type="pers.congcong.myBatis2.pojos.SysUser">
        <collection property="sysRoleList" columnPrefix="role_" resultMap="pers.congcong.myBatis2.mappers.RoleMapper.roleMap"/>
    </resultMap>

    <select id="selectAllUserAndRoles" resultMap="userRoleListMap">
        SELECT
            u.id,
            u.user_name,
            u.user_password,
            u.user_email,
            u.user_info,
            u.head_img,
            u.create_time,
            r.id "role_id",
            r.role_name "role_role_name",
            r.enabled "role_enabled",
            r.create_by "role_create_by",
            r.create_time "role_create_time"
        FROM sys_user u
            INNER JOIN sys_user_role ur on u.id = ur.user_id
            INNER JOIN sys_role r on ur.role_id = r.id
    </select>

然后在UserMapper中添加selectAllUserAndRoles接口

    /**
     * 选择role的List
     * @return
     */
    List<SysUser> selectAllUserAndRoles();

测试代码跟上面的差不多,就不占有版面了。
需要注意的是在配置resultMap的时候,需要配置id,
没有配置id时,MyBatis就会把resultMap中配置的所有字段进行比较,如果所有字段的值都相同就合并,只要有一个字段不同,就不合并。

接着添加第二层一对多的关系,Role跟privilege的关系:
在Role类中添加privilege集合,getter,setter方法:

List<SysPrivilege> privileges;
    public List<SysPrivilege> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(List<SysPrivilege> privileges) {
        this.privileges = privileges;
    }

然后添加PrivilegeMapper.xml,####记得要注意xml的文件名跟包路劲要跟对应接口的接口名和路径一致

<mapper namespace="pers.congcong.myBatis2.mappers.PrivilegeMapper">
  <resultMap id="privilegeMap" type="pers.congcong.myBatis2.pojos.SysPrivilege">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="privilege_name" jdbcType="VARCHAR" property="privilegeName" />
    <result column="privilege_url" jdbcType="VARCHAR" property="privilegeUrl" />
  </resultMap>

在RoleMapper.xml中添加resultMap配置。

    <resultMap id="rolePrivilegeListMap" type="pers.congcong.myBatis2.pojos.SysRole" extends="roleMap">
        <collection property="privileges" columnPrefix="privilege_"
                    resultMap="pers.congcong.myBatis2.mappers.PrivilegeMapper.privilegeMap"/>
    </resultMap>

最后修改SQL进行关联。
这个SQL看起来有点别扭:

   <select id="selectAllUserAndRoles" resultMap="userRoleListMap">
        SELECT
            u.id,
            u.user_name,
            u.user_password,
            u.user_email,
            u.user_info,
            u.head_img,
            u.create_time,
            r.id "role_id",
            r.role_name "role_role_name",
            r.enabled "role_enabled",
            r.create_by "role_create_by",
            r.create_time "role_create_time",
            p.id role_privilege_id,
            p.privilege_name role_privilege_privilege_name,
            p.privilege_url role_privilege_privilege_url
        FROM sys_user u
            INNER JOIN sys_user_role ur on u.id = ur.user_id
            INNER JOIN sys_role r on ur.role_id = r.id
            INNER JOIN sys_role_privilege rp on rp.role_id = r.id
            INNER JOIN sys_privilege p on p.id = rp.privilege_id
    </select>

最后写一个测试类:

    @Test
    public void testSelectAllUserAndRoles() {
        SqlSession sqlSession = getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            List<SysUser> sysUserList = userMapper.selectAllUserAndRoles();

            Assert.assertNotNull(sysUserList);
        } finally {
            sqlSession.rollback();
            sqlSession.close();
        }
    }

debug一下,运行结果如图:


两次一对多映射

collection集合的嵌套查询

我们知道association关联的嵌套查询这种方式会执行额外的SQL查询,映射配置会简单很多。

下面以自下而上的过程来实现这样一个两层嵌套的功能,由于面板关系,这里我只贴一层的代码。

    <resultMap id="rolePrivilegeListMapSelect" extends="roleMap" type="pers.congcong.myBatis2.pojos.SysRole">
        <collection property="privileges" fetchType="lazy" column="{roleId=id}" select="pers.congcong.myBatis2.mappers.PrivilegeMapper.selectPrivilegeByRoleId"/>
    </resultMap>

    <select id="selectRoleByUserId" resultMap="rolePrivilegeListMapSelect">
        SELECT
          r.id,
          r.role_name,
          r.enabled,
          r.create_time,
          r.create_by
        FROM sys_role r
        INNER JOIN sys_user_role ur ON ur.role_id = r.id
        WHERE ur.user_id = #{userId}
    </select>

PrivilegeMapper跟UserMapper里面的嵌套也是这样写

然后添加接口,怎样添加接口上面也讲过了,直接看测试结果图吧:


MyBatis的三层嵌套查询

通过这几个例子,相信我应该对这种映射结果的配置略懂一二了,这里值得注意的是collection的用法跟association的用法。还有延迟加载。OK,码完了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,175评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,674评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,151评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,597评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,505评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,969评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,455评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,118评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,227评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,213评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,214评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,928评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,512评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,616评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,848评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,228评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,772评论 2 339

推荐阅读更多精彩内容