mybatis筆記四

記錄下動態sql的常用標籤:

1.where

一般用作數據操作添加的條件

例子:

 <select id="selectByRoleId" resultMap="resource">
        select * from resource 
        <where>
            role_id = #{roleId}
        </where>

2.if

一般用做查詢,修改或者刪除數據時的一些拼接條件。

test字段爲判斷條件

例子:

<select id="findCountByContditon" resultType="int" parameterType="com.example.demo.po.User">
        select
        coalesce(count(id),0)
        from user
        <where>
            <if test="name != null and name != ''">
                name like  #{name}
            </if>
            <if test="createTime != null">
                and create_time  &lt; #{createTime}
            </if>
        </where>
    </select>

上面的sql有個問題,就是如果name條件不成立,creatTime條件成立,那麼sql會報錯。一般如果在where條件下有多個if判斷,在if前加入where 1=1。

<select id="findCountByContditon" resultType="int" parameterType="com.example.demo.po.User">
        select
        coalesce(count(id),0)
        from user
        <where>
            1=1
            <if test="name != null and name != ''">
                and name like  #{name}
            </if>
            <if test="createTime != null">
                and create_time  &lt; #{createTime}
            </if>
        </where>
    </select>

3.foreach

一般用來批量添加數據

collection字段一般爲list、array、map類型,值爲傳入的參數

separator字段值爲分隔符,即以什麼來分割

item 字段爲循環時每個元素的別名

index 字段值爲每次循環時的下標

open 字段值爲前綴值

close 字段值爲後綴值

例子:

<insert id="saveAll" parameterType="java.util.List">
        insert into user(name,role_id,password)
        values
        <foreach collection="users" separator="," item="user" index="index" >
            ( #{user.name}, #{user.roleId}, #{user.password})
        </foreach>
    </insert>

4.set

一般用在修改數據,與if經常一起出現

<update id="updateByCondition">
        update user
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
            <if test="createTime != null">
                create_time = @{createTime}
            </if>
        </set>
        where id = #{id}
    </update>

5.include

一般是將常用的字段抽出來作爲常量

<sql id="user_columns">
        id, name,role_id,password,create_time,update_time
    </sql>

<select id="selectById" resultMap="user">
        select 
         <include refid="user_columns"/>
         from user where id = #{id}
    </select>

6.trim

prefix字段前綴添加值

suffix字段後綴添加值

prefixOverrides字段刪除前綴值

suffixOverrides字段刪除後綴值

<update id="updateByCondition">
        update user  set
        <trim suffixOverrides=",">
            
                <if test="name != null and name != ''">
                    name = #{name},
                </if>
                <if test="password != null and password != ''">
                    password = #{password},
                </if>
                <if test="createTime != null">
                    create_time = @{createTime},
                </if>
            </trim>
        where id = #{id}
    </update>

如果password爲空,其他不爲空。sql變爲

update user set name = #{name}, create_time = #{createTime} where id = #{id }

 

有興趣學習的可以關注公衆號閱讀相關:碼上行走

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