Mybatis從入門到精通——動態SQL標籤(10)

一、動態sql標籤作用

動態SQL標籤是Mybatis的一大特色,通過使用動態SQL標籤可以完成一些稍微複雜的操作和簡化開發。

動態SQL標籤主要包括:if、where、set、trim、choose-when-otherwise、foreach,下面將對每個標籤進行說明。

 

二、where標籤

說明:where標籤一般和if標籤搭配使用,用於sql中存在多餘字符and或or的問題,當使用where標籤時,如果第一行多出了and或or則會自動去除,並且where之後如果不存在任何條件,則會自動去除where字符。

where標籤使用方式:

    <where>

          and 條件1

          and 條件2

    </where>

案例:

    <select id="selectByPerson" resultType="com.my.entity.Person" parameterType="com.my.entity.Person">
        select
        <include refid="Base_Column" />
        from person
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="telephone != null and telephone != ''">
                and telephone = #{telephone}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>
    </select>

三、if標籤

說明:if標籤用於做簡單的條件判斷,如果返回true,則使用if標籤體內的sql,如果不符合就不使用if標籤體內的sql;如果要判斷某個屬性或參數是否符合直接寫其參數名即可,不需要加#{}。

if標籤使用方式:

    <if test="條件">

          sql內容

    </if>

注意:if標籤進行與條件使用 and 字符,進行或條件使用 or 字符。

案例:

    <select id="selectByPerson" resultType="com.my.entity.Person" parameterType="com.my.entity.Person">
        select
        <include refid="Base_Column" />
        from person
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="telephone != null and telephone != ''">
                and telephone = #{telephone}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>
    </select>

四、set標籤

說明:set標籤作用和where標籤類似,用於處理update標籤中set後多個賦值多出逗號(,)字符的問題,一般和if標籤搭配使用,如果set後的條件都沒有,則會把set字符刪除。

set標籤使用方式:

    <set>

        賦值1,

        賦值2,

    </set>

案例:

    <update id="updateById" parameterType="com.my.entity.Person">
        update person
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="telephone != null and telephone != ''">
                telephone = #{telephone},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
        </set>
        where id = #{id}
    </update>

五、trim標籤

說明:用於添加前綴和後綴,並且能去除頭部字符和尾部字符,功能強大,任何地方都可以使用。

trim標籤的使用方式:

    <trim prefix="添加的前綴字符" suffix="添加的後綴字符" suffixOverrides="去除的尾部字符" prefixOverrides="去除的頭部字符">

        sql內容

    </trim>

注意:並不是所有的屬性都需要用到,只使用需要的屬性即可。

案例:

    <insert id="insert" parameterType="com.my.entity.Person" keyProperty="id" useGeneratedKeys="true">
        insert into person
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null">
                id,
            </if>
            <if test="name != null and name != ''">
                name,
            </if>
            <if test="sex != null and sex != ''">
                sex,
            </if>
            <if test="address != null and address != ''">
                address,
            </if>
            <if test="telephone != null and telephone != ''">
                telephone,
            </if>
            <if test="age != null">
                age,
            </if>
        </trim>
        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null and name != ''">
                #{name},
            </if>
            <if test="sex != null and sex != ''">
                #{sex},
            </if>
            <if test="address != null and address != ''">
                #{address},
            </if>
            <if test="telephone != null and telephone != ''">
                #{telephone},
            </if>
            <if test="age != null">
                #{age},
            </if>
        </trim>
    </insert>

五、foreach標籤

說明:用於遍歷操作,用於對List或Array或Set等集合和數組的遍歷操作。

foreach標籤的使用方式:

    <foreach collection="array|list|collection|屬性名" open="前綴" close="後綴" item="每一個元素的變量名" separator="分隔符" index="下標的變量名">

       sql內容

    </foreach>

該遍歷一般用於select in語句的遍歷以及各種批量操作。

案例:

    <select id="selectListByIds" resultMap="BaseResult">
        select
        <include refid="Base_Column" />
        from person
        <where>
            id in
            <foreach collection="array" open="(" close=")" item="id" separator="," index="i">
                #{id}
            </foreach>
        </where>
    </select>

六、choose-when-otherwise標籤

說明:該標籤類似java的switch case的操作,只要滿足其中一個when的條件,就只執行該when標籤內的sql,其它when和otherwise的則不管,when相當於case,用於條件判斷,otherwise則相當於when條件都不滿足時執行的操作,類似switch的default。

choose-when-otherwise標籤的使用方式:

    <choose>

        <when test="條件判斷1">

            sql內容

        </when>

        <when test="條件判斷2">

            sql內容

        </when>

        <otherwise> 

            默認的sql內容

        </otherwise>

    </choose>

注意:when標籤中的test屬性和if標籤的test屬性操作一致,可以引用參數進行判斷。

案例:

    <select id="selectByNameOrSexOrAge" resultMap="BaseResult">
        select
        <include refid="Base_Column" />
        from person
        <where>
            <choose>
                <when test="name != null and name != ''">
                    and name = #{name}
                </when>
                <when test="sex != null and sex != ''">
                    and sex = #{sex}
                </when>
                <when test="age != null">
                    and age = #{age}
                </when>
                <otherwise>
                    <!-- 什麼都不做 -->
                </otherwise>
            </choose>
        </where>
    </select>

七、補充說明

一般都是多個標籤配合使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章