zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    android 使用adb重新建立 eclipse和模拟器间的连接
    android ADB server didn't ACK
    Android getSystemService()
    Android隐藏标题栏
    Android 与WCF REST 服务联调
    Eclipase 无法启动,启动界面显示完版本号之后无响应
    调用WCF REST服务时,使用JSON
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    SqlServer跨域查询
    ASP.net导出Excel的几种方式
  • 原文地址:https://www.cnblogs.com/watermelonban/p/7601066.html
Copyright © 2011-2022 走看看