Mybatis的mapper.xml中<collection></collection>的用法

在mapper.xml文件中,我们在使用collection时有两种用法。这里做一下简单记录:

1、直接将collection集合元素的属性写为collection的字标签

如下:

<resultMap type="com.space.shiro.bean.User" id="userMap">
        <id property="id" column="uid"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <collection property="roles" ofType="com.space.shiro.bean.Role">
            <id property="id" column="rid"/>
            <result property="name" column="rname"/>
            <collection property="permissions" ofType="com.space.shiro.bean.Permissions">
                <id property="id" column="pid"/>
                <result property="name" column="pname"/>
            </collection>
        </collection>
    </resultMap>

它们的关系是这样的:

User里有一个Set<Role> roles

Role里有一个Set<Permissions> permissions

通过这样的配置,我们在执行查询User时,通过多表联查,就可以将这些级联属性全部关联查出。

下面是查询语句:

<select id="queryUserName" parameterType="string" resultMap="userMap">
        SELECT u.*,r.*,p.* FROM user u inner join user_role ur on ur.uid=u.uid
        inner join role r on r.rid=ur.rid
        inner join permissions_role pr on pr.rid=r.rid
        inner join permissions p on pr.pid=p.pid
        WHERE username=#{username};
</select>

2、通过在collection标签中引用别的mapper的查询方法

<resultMap id="BaseResultMap" type="com.space.sbsecurity.bo.sys.SysUser" >
        <id column="user_id" property="id" jdbcType="BIGINT" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <collection property="sysRoles" column="user_id"
                    select="com.space.sbsecurity.mapper.sys.SysRoleMapper.selectRoleListByUserId">
        </collection>
</resultMap>

在SysUser中有Set<SysRole> sysRoles

我们不需要再在collection中配置SysRole的属性,只需要将SysRole中的selectRoleListByUserId方法引入就可以了。

以下是查询语句:我们只需要查询SysUser就行了

<select id="findByUsername" resultMap="BaseResultMap">
        SELECT
            us.id as user_id,
            us.username,
            us.password
        FROM t_sys_user us  WHERE us.username = #{username}
</select>

当然,在SysRole的mapper中,我们是需要有selectRoleListByUserId方法的:

   <resultMap id="roleResult" type="com.space.sbsecurity.bo.sys.SysRole">
        <id property="id" column="role_id" jdbcType="BIGINT"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="desc" column="desc" jdbcType="VARCHAR"/>
        <collection property="permissions" column="role_id"
                    select="com.space.sbsecurity.mapper.sys.SysPermissionMapper.selectPermissionByRoleId">
        </collection>
    </resultMap>

    <select id="selectRoleListByUserId" resultMap="roleResult">
        SELECT
            ro.id as role_id,
            ro.name,
            ro.desc
        FROM  t_sys_user_role ur
         LEFT JOIN t_sys_role  ro
        ON  ur.`role_id` = ro.`id` WHERE ur.user_id = #{userId}
    </select>
同理,SysRole中的permissions也是一样的。


两种方式的实现都可以,但是博主跟喜欢第二种方式。因为我们不需要写过于复杂的sql,同时,每个mapper中的方法都是独立可以使用的,其适用性更强。


转载请务必保留此出处(原作者):https://blog.csdn.net/zhuzhezhuzhe1


版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。

https://blog.csdn.net/zhuzhezhuzhe1


猜你喜欢

转载自blog.csdn.net/zhuzhezhuzhe1/article/details/80588432