zoukankan      html  css  js  c++  java
  • MyBatis动态添加—trim标签

    做添加时,部分字段有值,没值的字段不添加,这就是动态添加,使用 trim 标签就可以实现。

    <insert id="insertSysUser" parameterType="com.mydemo.entity.SysUser">
            insert into sys_user
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">
                    id,
                </if>
                <if test="username != null and username != ''">
                    username,
                </if>
                <if test="realName != null and realName != ''">
                    real_name,
                </if>
                <if test="idcard != null and idcard != ''">
                    idcard,
                </if>
                create_time
            </trim>
            <trim prefix="values(" suffix=")" suffixOverrides=",">
                   <if test="id != null and id != ''">
                    #{id, jdbcType = VARCHAR},
                </if>
                <if test="username != null and username != ''">
                    #{username, jdbcType = VARCHAR},
                </if>
                <if test="realName != null and realName != ''">
                    #{realName, jdbcType = VARCHAR},
                </if>
                <if test="idcard != null and idcard != ''">
                    #{idcard, jdbcType = VARCHAR},
                </if>
                now()
            </trim>
        </insert>

    最终结果:insert into sys_user ( id, username, real_name, idcard, create_time ) values( ?, ?, ?, ?, now() ) 

    trim标签属性含义:

    属性 描述
    prefix 给sql语句拼接的前缀
    suffix 给sql语句拼接的后缀
    prefixesToOverride 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixesToOverride属性指定,假设该属性指定为”AND”,当sql语句的开头为”AND”,trim标签将会去除该”AND”
    suffixesToOverride 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixesToOverride属性指定

     

     

     

     

     

     

  • 相关阅读:
    Matplotlib
    Numpy&Pandas
    莫凡《机器学习》笔记
    李宏毅《1天搞懂深度学习》笔记
    Git客户端使用教程
    像素级压缩感知图像融合的论文
    二分图【洛谷P2175】 小Z的游戏分队
    模板【洛谷P3390】 【模板】矩阵快速幂
    模板 可并堆【洛谷P3377】 【模板】左偏树(可并堆)
    LCA SP913 QTREE2
  • 原文地址:https://www.cnblogs.com/jingyi17/p/10273181.html
Copyright © 2011-2022 走看看