原文 https://www.jianshu.com/p/e141a3c76ec7sql
1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>mybatis
prefix:在trim標籤內sql語句加上前綴。ide
suffix:在trim標籤內sql語句加上後綴。it
prefixOverrides:指定去除多餘的前綴內容test
suffixOverrides:指定去除多餘的後綴內容,如:suffixOverrides=",",去除trim標籤內sql語句多餘的後綴","。date
2.下面是一個往購物車表中插入數據的mybatis語句jdbc
<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>
假設沒有指定sql語句
suffixOverrides=","im
執行的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);這樣就將「,」去掉了。
前綴也是一個道理這裏就不說了。