【笔记】Mybatis高级查询(小结)--嵌套查询及延迟加载

<association><collection>标签一对一、一对多,多对多查询时用到的属性

  • property:对应实体类中的属性名,必填项。
  • javaType:属性对应的类型。
  • resultMap:可以直接使用现有的resultMap,不需要再配置。
  • columnPrefix:查询列的前缀,配置前缀后,在子标签配置<result>的column时可省略前缀。

1、<association>一对一的映射的使用例子

  • 在SysUser类中加入一对一的属性role
	/**
     * 假设一个用户只有一个角色(使用自动映射处理一对一关系)
     */
    private SysRole role;

    public SysRole getRole() {
		return role;
	}

	public void setRole(SysRole role) {
		this.role = role;
	}
  • javaType的使用例子:
  <!-- 使用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的使用例子(推荐使用):
  <!-- 使用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>
  • 查询的SQL(适用于一条SQL查询出所有结果的场景)
  <!-- 使用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>一对多、多对多的映射查询使用例子

  • 在SysUser类中加入一对多的属性roles
	/**
     * 用户的角色集合(一个用户可以有多个角色)
     */
    private List<SysRole> roles;

	public List<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}
  • javaType的使用例子
  <!-- 使用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的使用例子(推荐使用)
  <!-- 使用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>
  • 查询的SQL(适用于一条SQL查询出所有结果的场景)
  <!-- 使用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>标签实现嵌套查询,需要用到以下属性:

  • select:另一个映射查询的ID,Mybatis会额外执行这个查询获取嵌套对象的结果。

  • column:列名或别名,将主查询中列的结果作为嵌套查询的参数,配置方式如column={prop1=col1,prop2=col2},prop1和prop2作为嵌套查询的参数。

  • fetchType:数据的加载方式,可选值为lazy和eager,分别为延迟加载和积极加载。

	<!--  延迟加载与全局配置aggressiveLazyLoading有关,需要设为false-->
	<settings>
		<!-- 延迟加载配置,3.4.5版本默认为false,当为true时<association>标签的fetchType="lazy"无效,要生效必须为false-->
		<setting name="aggressiveLazyLoading" value="false" />
	</settings>

1、<association>一对一嵌套查询及延迟加载使用例子

  • 角色子查询方法selectRoleById(根据角色编号查询角色信息)
  <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>
  • 主查询方法selectRoleById(根据角色编号查询角色信息)
  <!-- 使用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>一对多嵌套查询及延迟加载使用例子

  • 权限子查询方法selectPriByRoleId(根据角色编号查询角色权限)
  <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>
  • 角色子查询方法selectRolesById(根据用户编号查询角色信息)
  <!-- 使用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>
  • 用户主查询方法selectRolesById(根据用户编号查询用户信息)
  <!-- 使用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>

猜你喜欢

转载自blog.csdn.net/q283614346/article/details/83316164