<?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="com.sf.mapper.SysUserMapper">
    <!--以map形式返回的结果-->
    <resultMap id="UserAndInfoMap" type="sysUser">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <association property="userInfo" javaType="sysUserInfo">
            <id column="info_id" property="infoId"/>
            <result column="real_name" property="realName"/>
            <result column="user_id" property="userId"/>
            <result column="phone" property="phone"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
            <result column="address" property="address"/>
        </association>
    </resultMap>

    <!--sql语句片段-->
    <sql id="findUserAndInfo">
        SELECT u.*,ui.*
        FROM sys_user u
        LEFT JOIN sys_user_info ui
        ON u.user_id=ui.user_id
    </sql>

    <select id="findAll" resultMap="UserAndInfoMap">
        /*引入sql语句片段*/
        <include refid="findUserAndInfo"/>
    </select>

    <select id="findById" resultMap="UserAndInfoMap" parameterType="integer">
        <include refid="findUserAndInfo"/>
        /*添加条件*/
        <where>
            u.user_id=#{userId}
        </where>
    </select>

    <resultMap id="userWithInfoAndRoleMap" type="sysUser" extends="UserAndInfoMap">
        <association property="role" javaType="sysRole">
            <id column="role_id" property="roleId"/>
            <result column="role_name" property="roleName"/>
            <result column="role_code" property="roleCode"/>
            <result column="description" property="description"/>
            <result column="create_time" property="createTime"/>
        </association>
    </resultMap>
    <select id="findUserWithInfoAndRole" resultMap="userWithInfoAndRoleMap" parameterType="integer">
        SELECT u.*,ui.*,r.*
        FROM sys_user u
        LEFT JOIN sys_user_info ui ON u.user_id=ui.user_id
        LEFT JOIN sys_role r ON u.role_id=r.role_id
        WHERE u.user_id=#{userId}
    </select>
</mapper>