mybatis之trim

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

prefix:在trim标签内sql语句加上前缀。

suffix:在trim标签内sql语句加上后缀。

suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。

prefixOverrides:指定去除多余的前缀内容

2.下面是一个往购物车表中插入数据的mybatis语句

[java] view plain copy

  1. <insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">  
  2.         insert into cart  
  3.         <trim prefix="(" suffix=")" suffixOverrides=",">  
  4.             <if test="id != null">  
  5.                 id,  
  6.             </if>  
  7.             <if test="userId != null">  
  8.                 user_id,  
  9.             </if>  
  10.             <if test="dealId != null">  
  11.                 deal_id,  
  12.             </if>  
  13.             <if test="dealSkuId != null">  
  14.                 deal_sku_id,  
  15.             </if>  
  16.             <if test="count != null">  
  17.                 count,  
  18.             </if>  
  19.             <if test="createTime != null">  
  20.                 create_time,  
  21.             </if>  
  22.             <if test="updateTime != null">  
  23.                 update_time,  
  24.             </if>  
  25.         </trim>  
  26.         <trim prefix="values (" suffix=")" suffixOverrides=",">  
  27.             <if test="id != null">  
  28.                 #{id,jdbcType=BIGINT},  
  29.             </if>  
  30.             <if test="userId != null">  
  31.                 #{userId,jdbcType=BIGINT},  
  32.             </if>  
  33.             <if test="dealId != null">  
  34.                 #{dealId,jdbcType=BIGINT},  
  35.             </if>  
  36.             <if test="dealSkuId != null">  
  37.                 #{dealSkuId,jdbcType=BIGINT},  
  38.             </if>  
  39.             <if test="count != null">  
  40.                 #{count,jdbcType=INTEGER},  
  41.             </if>  
  42.             <if test="createTime != null">  
  43.                 #{createTime,jdbcType=TIMESTAMP},  
  44.             </if>  
  45.             <if test="updateTime != null">  
  46.                 #{updateTime,jdbcType=TIMESTAMP},  
  47.             </if>  
  48.         </trim>  
  49.     </insert>  

假设没有指定[java] view plain copy

  1. 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);这样就将“,”去掉了。

前缀也是一个道理这里就不说了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那些年的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值