MyBatis高级查询

作者: JaJa大宝剑 | 来源:发表于2018-05-25 17:32 被阅读0次

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

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

    使用自动映射就是通过别名让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,码完了。

    相关文章

      网友评论

        本文标题:MyBatis高级查询

        本文链接:https://www.haomeiwen.com/subject/vtkzrftx.html