zoukankan      html  css  js  c++  java
  • mybatis中<trim prefix="VALUES (" suffix=")" suffixOverrides=",">

      这个标签使用的有些少,但是想写通用一点的sql时,用起来还是挺好的。

      在后面的说明中,遇到一个坑,刚刚修改了一下,写的多了才会遇见问题。

    1.说明

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

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

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

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

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

    2.示例

      在本示例中,因为在后面有三个字段,所以前面的都可加上逗号,也不会出现问题。

      如果没有这三个字段,前面第几个开始不加逗号,说不准,这个时候,就需要使用suffixOverrides去除。

    <insert id="save" useGeneratedKeys="true" keyProperty="id">
            INSERT INTO t_contract_monitor
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="contractId != null">
                    contract_id,
                </if>
                <if test="contractNo != null">
                    contract_no,
                </if>
                <if test="merchantId != null">
                    merchant_id,
                </if>
                gmt_create,
                gmt_modify,
                is_deleted
            </trim>
            <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
                <if test="contractId != null">
                    #{contractId,jdbcType=BIGINT},
                </if>
                <if test="contractNo != null">
                    #{contractNo,jdbcType=VARCHAR},
                </if>
                <if test="merchantId != null">
                    #{merchantId,jdbcType=BIGINT},
                </if>
                now(), now(), 0
            </trim>
        </insert>
    

      

    3.另外说明

      useGeneratedKeys="true" keyProperty="id"

      当我们insert时,返回剛剛新增的id,可以加上上面的两个属性。

      主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值,不是数据库表中的字段名

      但是,需要有一些注意:

    Long save = mcsContractBillTermRecordMapper.save(mcsContractBillTermRecord);
    

      这个save是新增的个数,那新增id返回值在哪里呢,在mcsContractBillTermRecord中,自己get。

  • 相关阅读:
    async await promise写法
    nginx自动启动脚本
    nginx源码编译安装
    PHP源码编译安装
    MySQL修改root密码的多种方法
    PKG_CONFIG_PATH变量 与 ld.so.conf 文件
    confluence5.65+CentOS+mysql安装破解
    nigos core 安装配置
    cacti+CentOS6.5
    Linux+mysql+apache+php
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12469304.html
Copyright © 2011-2022 走看看