【笔记】Mybatis高级查询(准备)
【笔记】Mybatis高级查询(一)–使用自动映射处理一对一关系
【笔记】Mybatis高级查询(二)–使用resultMap配置一对一映射
【笔记】Mybatis高级查询(三)–使用标签实现嵌套查询及延迟加载
【笔记】Mybatis高级查询(四)–使用resultMap的标签实现一对多和多对多查询
【笔记】Mybatis高级查询(五)–使用resultMap的进行嵌套查询及延迟加载
【笔记】Mybatis高级查询(六)–鉴别器discrimiator的使用
【笔记】Mybatis高级查询(七)–存储过程调用
【笔记】Mybatis高级查询(八)–枚举处理器的使用
【笔记】Mybatis高级查询(九)–Mybatis代码生成器的使用
【笔记】Mybatis高级查询(十)–Mybatis缓存使用
<association>
与<collection>
标签一对一、一对多,多对多查询时用到的属性
1、<association>
一对一的映射的使用例子
/**
* 假设一个用户只有一个角色(使用自动映射处理一对一关系)
*/
private SysRole role;
public SysRole getRole() {
return role;
}
public void setRole(SysRole role) {
this.role = role;
}
<!-- 使用resultMap的association标签配置一对一映射 -->
<resultMap id="userRoleMap3" extends="userMap" type="ex.mybatis.rbac.model.SysUser">
<!-- role相关属性,前缀都加上了r_ -->
<association property="role" columnPrefix="r_" javaType="ex.mybatis.rbac.model.SysRole">
<result column="id" property="id" />
<result column="role_name" property="roleName" />
<result column="enabled" property="enabled" />
<result column="create_by" property="createBy" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</association>
</resultMap>
<!-- 使用resultMap的association标签配置一对一映射 -->
<resultMap id="userRoleMap4" extends="userMap" type="ex.mybatis.rbac.model.SysUser">
<!-- role相关属性,直接通过resultMap属性引用roleMap,需要加上命名空间的前缀,列前缀都加上了r_ -->
<association property="role" columnPrefix="r_" resultMap="ex.mybatis.rbac.mapper.SysRoleMapper.roleMap" />
</resultMap>
<!-- 使用resultMap的association标签配置一对一映射 -->
<select id="selectUserAndRoleById3" resultMap="userRoleMap4">
select
u.id,
u.user_name,
u.user_password,
u.user_email,
u.create_time,
u.user_info,
u.head_img,
r.id r_id,
r.role_name r_role_name,
r.enabled r_enabled,
r.create_by r_create_by,
r.create_time r_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>
2、<collection>
一对多、多对多的映射查询使用例子
/**
* 用户的角色集合(一个用户可以有多个角色)
*/
private List<SysRole> roles;
public List<SysRole> getRoles() {
return roles;
}
public void setRoles(List<SysRole> roles) {
this.roles = roles;
}
<!-- 使用resultMap的association标签配置一对一映射 -->
<resultMap id="userRolesMap" extends="userMap" type="ex.mybatis.rbac.model.SysUser">
<!-- role相关属性,前缀都加上了r_ -->
<collection property="role" columnPrefix="r_" javaType="ex.mybatis.rbac.model.SysRole">
<result column="id" property="id" />
<result column="role_name" property="roleName" />
<result column="enabled" property="enabled" />
<result column="create_by" property="createBy" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</collection>
</resultMap>
<!-- 使用resultMap的<collection>标签进行一对多查询 -->
<resultMap id="userRolesMap" extends="userMap" type="ex.mybatis.rbac.model.SysUser">
<!-- 角色的集合 -->
<collection property="roles" columnPrefix="r_" resultMap="ex.mybatis.rbac.mapper.SysRoleMapper.roleMap"/>
</resultMap>
<!-- 使用resultMap的<collection>标签进行一对多查询 -->
<select id="selectUserAndRoles" resultMap="userRolesMap">
select
u.id,
u.user_name,
u.user_password,
u.user_email,
u.create_time,
u.user_info,
u.head_img,
r.id r_id,
r.role_name r_role_name,
r.enabled r_enabled,
r.create_by r_create_by,
r.create_time r_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>
<if test="id != null">
u.id = #{id}
</if>
</where>
</select>
<association>
与<collection>
标签实现嵌套查询,需要用到以下属性:
<!-- 延迟加载与全局配置aggressiveLazyLoading有关,需要设为false-->
<settings>
<!-- 延迟加载配置,3.4.5版本默认为false,当为true时<association>标签的fetchType="lazy"无效,要生效必须为false-->
<setting name="aggressiveLazyLoading" value="false" />
</settings>
1、<association>
一对一嵌套查询及延迟加载使用例子
<resultMap id="roleMap" type="ex.mybatis.rbac.model.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>
<!-- 根据角色编号查询角色信息-->
<select id="selectRoleById" resultMap="roleMap">
select id, role_name, enabled, create_by, create_time
from sys_role
where id = #{id}
</select>
<!-- 使用resultMap的association标签进行嵌套查询 -->
<resultMap id="userRoleMapSelect" extends="userMap" type="ex.mybatis.rbac.model.SysUser">
<!-- 嵌套查询role,column配置的是嵌套查询SQL的参数,当有多个时用逗号隔开, fetchType="lazy"实现延迟加载 -->
<association property="role" fetchType="lazy" column="{id=role_id}"
select="ex.mybatis.rbac.mapper.SysRoleMapper.selectRoleById"/>
</resultMap>
<!-- 使用resultMap的association标签进行嵌套查询 -->
<select id="selectUserAndRoleByIdSel" resultMap="userRoleMapSelect">
select
u.id,
u.user_name,
u.user_password,
u.user_email,
u.create_time,
u.user_info,
u.head_img,
ur.role_id
from sys_user u
inner join sys_user_role ur on u.id = ur.user_id
where u.id = #{id}
</select>
2、<collection>
一对多嵌套查询及延迟加载使用例子
<resultMap id="privilegeMap" type="ex.mybatis.rbac.model.SysPrivilege">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="privilege_name" jdbcType="VARCHAR" property="privilegeName" />
<result column="privilege_url" jdbcType="VARCHAR" property="privilegeUrl" />
</resultMap>
<!-- 根据角色id查询权限 -->
<select id="selectPriByRoleId" resultMap="privilegeMap">
select id, privilege_name, privilege_url
from sys_privilege p
inner join sys_role_privilege rp on rp.privilege_id = p.id
where rp.role_id = #{roleId}
</select>
<!-- 使用resultMap的<collection>标签进行一对多查询,根据用户查询角色信息 -->
<resultMap id="rolePrisMapSel" extends="roleMap" type="ex.mybatis.rbac.model.SysRole">
<!-- 角色权限的集合 -->
<collection property="privileges" fetchType="lazy" column="{roleId=id}"
select="ex.mybatis.rbac.mapper.SysPrivilegeMapper.selectPriByRoleId" />
</resultMap>
<!-- 使用resultMap的<collection>标签进行一对多查询,查询角色对应的权限 -->
<select id="selectRolesById" resultMap="rolePrisMapSel">
select
r.id,
r.role_name,
r.enabled,
r.create_by,
r.create_time
from sys_role r
inner join sys_user_role ur on ur.role_id = r.id
where ur.user_id = #{userId}
</select>
<!-- 使用resultMap的<collection>标签进行一对多查询 ,根据编号查用户信息-->
<resultMap id="userRolesMapSel" extends="userMap" type="ex.mybatis.rbac.model.SysUser">
<!-- 角色的集合 -->
<collection property="roles" fetchType="lazy" column="{userId=id}"
select="ex.mybatis.rbac.mapper.SysRoleMapper.selectRolesById"/>
</resultMap>
<!-- 使用resultMap的<collection>标签进行一对多查询,根据编号查用户信息 -->
<select id="selectUserById" resultMap="userRolesMapSel">
select
id,
user_name,
user_password,
user_email,
create_time,
user_info,
head_img
from sys_user
where id = #{id}
</select>