" type="hidden"/>

mybatis之


原文 https://www.jianshu.com/p/e141a3c76ec7

 

1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>

prefix:在trim標簽內sql語句加上前綴。

suffix:在trim標簽內sql語句加上后綴。


 

prefixOverrides:指定去除多余的前綴內容

suffixOverrides:指定去除多余的后綴內容,如:suffixOverrides=",",去除trim標簽內sql語句多余的后綴","。


 

 

 

2.下面是一個往購物車表中插入數據的mybatis語句

<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">

        insert into cart

        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">

                id,

            </if>

            <if test="userId != null">

                user_id,

            </if>

            <if test="dealId != null">

                deal_id,

            </if>

            <if test="dealSkuId != null">

                deal_sku_id,

            </if>

            <if test="count != null">

                count,

            </if>

            <if test="createTime != null">

                create_time,

            </if>

            <if test="updateTime != null">

                update_time,

            </if>

        </trim>

        <trim prefix="values (" suffix=")" suffixOverrides=",">

            <if test="id != null">

                #{id,jdbcType=BIGINT},

            </if>

            <if test="userId != null">

                #{userId,jdbcType=BIGINT},

            </if>

            <if test="dealId != null">

                #{dealId,jdbcType=BIGINT},

            </if>

            <if test="dealSkuId != null">

                #{dealSkuId,jdbcType=BIGINT},

            </if>

            <if test="count != null">

                #{count,jdbcType=INTEGER},

            </if>

            <if test="createTime != null">

                #{createTime,jdbcType=TIMESTAMP},

            </if>

            <if test="updateTime != null">

                #{updateTime,jdbcType=TIMESTAMP},

            </if>

        </trim>

    </insert>


假設沒有指定

suffixOverrides=","

執行的sql語句也許是這樣的:insert into cart (id,user_id,deal_id,) values(1,2,1,);顯然是錯誤的

指定之后語句就會變成insert into cart (id,user_id,deal_id) values(1,2,1);這樣就將“,”去掉了。

前綴也是一個道理這里就不說了。







 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM