<?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.lc.mybatis.mapper.UserMapper">

    <!--
        定义sql片段
        使用 include 引用refid sql片段id
     -->
    <sql id="userColumns">
        id,username,password,gender,address
    </sql>

    <!--
        if
         属性 test 返回 布尔类型。 true 执行  false 不执行
        test "el 表达式"  使用 属性  方法。
    -->
    <select id="getByUser"  resultType="com.lc.mybatis.entity.User" >
        SELECT
            <include refid="userColumns"></include>
        FROM
            `t_user`

        where 1=1

        <if test=" username != null and username != ''  ">
            and  username = #{username}
        </if>
        <if test=" gender != null and gender != ''  ">
            and  gender = #{gender}
        </if>
        <if test=" address != null and address != ''  ">
            and  address = #{address}
        </if>


    </select>


    <!--
        choose、when、otherwise 语句
        choose 包裹
            when 判断 注意 执行其中一个。
            otherwise 默认执行
     -->
    <select id="getByUserChoose"  resultType="com.lc.mybatis.entity.User" >
        SELECT
        *
        FROM
        `t_user`

        where 1=1

        <choose>
            <when test=" username != null and username != '' ">
                and  username = #{username}
            </when>
            <when test=" gender != null and gender != '' ">
                and  gender = #{gender}
            </when>
            <otherwise>
                and address = #{address}
            </otherwise>
        </choose>
    </select>

    <!--  where 标签  去除and 和 添加 where  -->
    <select id="getByUserWhere"  resultType="com.lc.mybatis.entity.User" >
        SELECT
        *
        FROM
        `t_user`

        <where>
            <if test=" username != null and username != ''  ">
                username = #{username}
            </if>
            <if test=" gender != null and gender != ''  ">
                and  gender = #{gender}
            </if>
            <if test=" address != null and address != ''  ">
                and  address = #{address}
            </if>
        </where>

    </select>

    <!--
      trim 替换
        prefix 前缀
        prefixOverrides 覆盖前缀
        suffix 后缀
        suffixOverrides 覆盖后缀
     -->

    <select id="getByUserTrim01"  resultType="com.lc.mybatis.entity.User" >
        SELECT
        *
        FROM
        `t_user`
        <trim prefix="where" prefixOverrides="and"  >
            <if test=" username != null and username != ''  ">
               and username = #{username}
            </if>
            <if test=" gender != null and gender != ''  ">
                and  gender = #{gender}
            </if>
            <if test=" address != null and address != ''  ">
                and  address = #{address}
            </if>
        </trim>

    </select>


    <!--
        set 添加 set 去除 ,
           修改
     -->
    <update id="updateUserSet">

        update t_user
        <set>
            <if test=" username != null and username != ''  ">
                username = #{username} ,
            </if>
            <if test=" gender != null and gender != ''  ">
                gender = #{gender} ,
            </if>
            <if test=" address != null and address != ''  ">
                address = #{address} ,
            </if>
            <if test=" password != null and password != ''  ">
                password = #{password} ,
            </if>
        </set>

        where id = #{id}
    </update>

    <update id="updateUserTrim02">

        update t_user

        <trim prefix="set"  suffixOverrides=","  >
            <if test=" username != null and username != ''  ">
                username = #{username} ,
            </if>
            <if test=" gender != null and gender != ''  ">
                gender = #{gender} ,
            </if>
            <if test=" address != null and address != ''  ">
                address = #{address} ,
            </if>
            <if test=" password != null and password != ''  ">
                password = #{password} ,
            </if>
        </trim>

        where id = #{id}
    </update>

    <!--
        foreach
            collection 集合名称
            open="" 开始符号
            separator="" 分隔符号
            close="" 关闭符号
            index=""  索引
            item=""  元素
     -->
    <insert id="insertBatch">
        INSERT INTO `vip33`.`t_user`
        VALUES
        <foreach collection="userList"  item="user"  separator=",">
            ( #{user.id}, null, #{user.username}, #{user.username},#{user.gender}, #{user.address} )
        </foreach>

    </insert>

    <delete id="deleteByIds">
        delete from  t_user
        where id in
        <foreach collection="ids" open="(" item="id"  separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 二级缓存 -->
    <cache ></cache>

    <!-- getById -->
    <select id="getById" resultType="com.lc.mybatis.entity.User">
        select  * from  t_user where id= #{id}
    </select>


    <!-- 查询分页  -->
    <select id="getUserList"  resultType="com.lc.mybatis.entity.User">
        select  * from  t_user
    </select>

</mapper>