• MyBatis高级查询 一对多映射


    数据库表在一对一映射中。

    在数据库sys_user_role中新增一条记录

    一个用户可以有多个角色。查询出所有用户和所对应的角色。

    1.collection集合的嵌套结果映射

    <!-- SysUserMapper.xml -->
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="test.dao.SysUserMapper">
      <resultMap id="BaseResultMap" type="test.model.SysUser">
        <!--
          WARNING - @mbggenerated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="user_password" jdbcType="VARCHAR" property="userPassword" />
        
        <result column="user_name" jdbcType="VARCHAR" property="userName" />
        <result column="user_email" jdbcType="VARCHAR" property="userEmail" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="user_info" jdbcType="LONGVARCHAR" property="userInfo" />
        <result column="head_img" jdbcType="LONGVARBINARY" property="headImg" />
        
      </resultMap>
      
      <!-- 继承上面的UserResultMap -->
      <resultMap type="test.model.SysUser" id="userRoleResultMap" extends="BaseResultMap">
          <!-- roleList相关属性 -->
        <collection property="roleList" resultMap="test.dao.SysRoleMapper.BaseResultMap" columnPrefix="role_">
        </collection>
      </resultMap>
      
      <select id="selectUserRoleListAll" resultMap="userRoleResultMap">
          select 
              u.id,
              u.user_name,
              u.user_password,
              u.user_email,
            u.create_time,
              u.user_info,
              u.head_img,
              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>
    </mapper>
  • 相关阅读:
    机器学习:特征选择方法简介
    VS快捷键
    非常适用的Sourceinsight插件,提高效率【强力推荐】
    Linux静态库和共享库
    C/C++ 位域知识小结
    __BEGIN_DECLS 和 __END_DECLS
    C语言可变参数va_list
    mac的terminal快捷键
    linux进程、线程与cpu的亲和性(affinity)
    C++ Singleton (单例) 模式最优实现
  • 原文地址:https://www.cnblogs.com/watermelonban/p/7601066.html
Copyright © 2020-2023  润新知