六、动态SQL

2018-09-12  本文已影响1人  Class鸣


if

普通的

    <select id="findByParamIf" resultType="us" parameterType="us">
        SELECT * FROM users 
        WHERE
        <!-- test 判断表达式(OGNL表达式) -->
            <if test="uid!=null">
                uid=#{uid}
            </if>
            <!-- 并且不等于空字符串 (& >> &quot;) -->
            <if test="uname!=null and uname!=''">
                and uname = #{uname}
            </if>
            <if test="birthday!=null">
                and birthday=#{birthday}
            </if>
    </select>

<where>封装查询条件

<select id="findByParamIf" resultType="us" parameterType="us">
        <!-- 
            查询的时候,缺失第一个条件是会出现SQL拼错
            解决方法 一 :
                * 给where 加上 1=1 之后的条件都可以 and XXX=XXX 
            解决方法 二 :
                * 使用<where> Xxx = Xxx </where>
            -->
        SELECT * FROM users
        WHERE 1=1
        <!-- test 判断表达式(OGNL表达式) -->
            <if test="uid!=null">
                and uid=#{uid}
            </if>
            <!-- 并且不等于空字符串 (&:&quot;) -->
            <if test="uname!=null and uname!=''">
                and uname like #{uname}
            </if>
            <if test="birthday!=null">
                and birthday=#{birthday}
            </if>
    </select>

<trim>

choose

    <!-- 查询 携带了哪个就只用哪个查(一次只能匹配一个参数) -->
    <select id="findByParamChoose" resultType="us" parameterType="us">
        SELECT * FROM users WHERE 1=1
            <!-- 只会选择一个参数查询 -->
            <choose>
                <!-- 第一步 -->
                <when test="uid!=null">
                    and uid=#{uid}
                </when>
                <!-- 第二步 -->
                <when test="uname!=null and uname!=''">
                    and uname like #{uname}
                </when>
                <otherwise>
                    <!-- 其他情况 -->
                </otherwise>
            </choose>
    </select>

set

    <!-- 修改 -->
    <update id="update" parameterType="us">
        <!-- 根据id 选择更新 -->
        UPDATE users
        <!-- 写更新的时候有可能多出逗号 -->
        <set>
            <if test="uname!=null">
                uname=#{uname},
            </if>
            <if test="birthday!=null">
                birthday=#{birthday},
            </if>
        </set>
        WHERE uid=#{uid}
    </update>

foreach

范围查找

<!-- 动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候(指定范围)
          用 foreach 来改写 select * from user where id in (1,2,3)
     -->
    <select id="findByParamForeach" resultType="us" parameterType="lsit">
        SELECT * FROM users  
        WHERE id 
        IN
        <!--
            collection:指定输入对象中的集合属性的名称
            item:每次遍历生成的对象
            open:开始遍历时的拼接字符串
            close:结束时拼接的字符串
            separator:遍历对象之间需要拼接的字符串
        -->
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            id=#{id}
        </foreach>
    </select>

批量插入

<!-- 批量保存 -->
    <insert id="saveList">
    INSERT INTO test.users (uname, birthday) 
    VALUES
        <foreach collection="list" item="us" open="" separator="," close="">
            (#{us.uname} , #{us.showBirthday})
        </foreach>
    </insert>

参数

databaseId=""

配合if判断数据库
<if test="databaseId == "mysql">

parameter=""

配合if判断是否传入参数
<if test="parameter != null>

bind

<bind name="_uname" value="'%'+uname+'%'">

sqlinclude

声明

<sql id="insert_select">select * from</sql>

插入

<include refid="insert_select"/>
上一篇下一篇

猜你喜欢

热点阅读