zoukankan      html  css  js  c++  java
  • Mybatis 动态insert语句

    mybatis的一个比较先进的思想是把Sql语句写在了配置xml文件(也支持注解),通过配置文件的方式,免去了一般软件开发的硬编码,当业务需求改变的时候,只需要更改sql语句即可!

    下面是个人在学习mybatis动态insert语句的笔记,留着参考!
    在写insert子句的时候,由于不知道需要插入多少字段,mybatis通过prefix,suffix,suffixOverrides很好的解决了该问题,实现了动态insert语句。

    <insert id="insertSelective" parameterType="com.bootdo.system.domain.LogDO">
    insert into sys_log
    <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
    id,
    </if>
    <if test="userId != null">
    user_id,
    </if>
    <if test="username != null">
    username,
    </if>
    <if test="operation != null">
    operation,
    </if>
    <if test="time != null">
    time,
    </if>
    <if test="method != null">
    method,
    </if>
    <if test="params != null">
    params,
    </if>
    <if test="ip != null">
    ip,
    </if>
    <if test="gmtCreate != null">
    gmt_create,
    </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="username != null">
    #{username,jdbcType=VARCHAR},
    </if>
    <if test="operation != null">
    #{operation,jdbcType=VARCHAR},
    </if>
    <if test="time != null">
    #{time,jdbcType=INTEGER},
    </if>
    <if test="method != null">
    #{method,jdbcType=VARCHAR},
    </if>
    <if test="params != null">
    #{params,jdbcType=VARCHAR},
    </if>
    <if test="ip != null">
    #{ip,jdbcType=VARCHAR},
    </if>
    <if test="gmtCreate != null">
    #{gmtCreate,jdbcType=TIMESTAMP},
    </if>
    </trim>
    </insert>

  • 相关阅读:
    解决Windows Server2008 R2中IE开网页时弹出阻止框
    为Java说句公道话
    垃圾回收(GC)的三种基本方式
    偏执却管用的10条Java编程技巧
    学习Javascript的8张思维导图【收藏】
    Java 常见异常及趣味解释
    Java程序员们最常犯的3个集合错误
    浅谈jsp、freemarker、velocity区别
    ThreadLocal使用
    javascript 之闭包
  • 原文地址:https://www.cnblogs.com/lywJ/p/10309043.html
Copyright © 2011-2022 走看看