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

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

---------------------

作者:醒悟wjn

来源:CSDN

原文:https://blog.csdn.net/qq_33054511/article/details/70490046

版权声明:本文为博主原创文章,转载请附上博文链接!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是mybatis MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis...
    seadragonnj阅读 2,344评论 0 7
  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,111评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,742评论 18 399
  • MYSQL 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 MySQL基本操...
    Kingtester阅读 7,848评论 5 116
  • ef
    红昔昔阅读 172评论 0 1