mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>

1. <trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容
2.下面是一个往新闻表中插入数据的mybatis语句
<insert id="insertSelective" parameterType="com.cn.module.news.entity.SwapNews" >
    insert into swap_news
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >#{id,jdbcType=VARCHAR},</if>
      <if test="id == null" >UUID(),</if>
      <if test="type != null" >
        type,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="stitle != null" >
        stitle,
      </if>
      <if test="author != null" >
        author,
      </if>
      <if test="souce != null" >
        souce,
      </if>
      <if test="tags != null" >
        tags,
      </if>
      <if test="summary != null" >
        summary,
      </if>
      <if test="content != null" >
        content,
      </if>
      <if test="picPath != null" >
        pic_path,
      </if>
      <if test="remarks != null" >
        remarks,
      </if>
      <if test="approvalFlag != null" >
        approval_flag,
      </if>
      <if test="isTop != null" >
        is_top,
      </if>
      <if test="delFlag != null" >
        del_flag,
      </if>
      <if test="createUser != null" >
        create_user,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updateUser != null" >
        update_user,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="type != null" >
        #{type,jdbcType=INTEGER},
      </if>
      <if test="title != null" >
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="stitle != null" >
        #{stitle,jdbcType=VARCHAR},
      </if>
      <if test="author != null" >
        #{author,jdbcType=VARCHAR},
      </if>
      <if test="souce != null" >
        #{souce,jdbcType=VARCHAR},
      </if>
      <if test="tags != null" >
        #{tags,jdbcType=VARCHAR},
      </if>
      <if test="summary != null" >
        #{summary,jdbcType=VARCHAR},
      </if>
      <if test="content != null" >
        #{content,jdbcType=LONGVARCHAR},
      </if>
      <if test="picPath != null" >
        #{picPath,jdbcType=VARCHAR},
      </if>
      <if test="remarks != null" >
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="approvalFlag != null" >
        #{approvalFlag,jdbcType=INTEGER},
      </if>
      <if test="isTop != null" >
        #{isTop,jdbcType=INTEGER},
      </if>
      <if test="delFlag != null" >
        #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="createUser != null" >
        #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

假设没有指定

suffixOverrides=","

执行的sql语句也许是这样的:insert into swap_news(id,type,title,) values(1,2,哈哈哈,);显然是错误的
指定之后语句就会变成insert into swap_news(id,type,title,) values(1,2,哈哈哈);这样就将“,”去掉了。
前缀也是一个道理这里就不说了。

猜你喜欢

转载自blog.csdn.net/dapyandxpy/article/details/79138985