【java】Mybatis 存在则修改,没有则新增/插入

  • 环境介绍:我的项目使用了 Java+ Mybatis,数据库是 MySQL

  • 实现目标:传入一个对象更新表数据,如果存在该主键的值则修改,没有的话则新增/插入
  • 下面是Mybatisxml 文件中的代码:

    <insert id="saveOneNull" useGeneratedKeys="true" keyProperty="id" keyColumn="id"

    parameterType="com.xdf.femisnsb.model.OrderDetail">

    insert INTO orderdetail

    <trim prefix="(" suffix=")" suffixOverrides=",">

    <if test="id != null">id,</if>

    <if test="mainid != null">mainid,</if>

    <if test="ordercode != null">ordercode,</if>

    <if test="goodsname != null">goodsname,</if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides=",">

    <if test="id != null">#{id,jdbcType=INTEGER},</if>

    <if test="mainid != null">#{mainid,jdbcType=INTEGER},</if>

    <if test="ordercode != null">#{ordercode,jdbcType=VARCHAR},</if>

    <if test="goodsname != null">#{goodsname,jdbcType=VARCHAR},</if>

    </trim>

    ON DUPLICATE KEY UPDATE

    <trim suffixOverrides=",">

    <if test="mainid != null">mainid = #{mainid,jdbcType=INTEGER},</if>

    <if test="ordercode != null">ordercode = #{ordercode,jdbcType=VARCHAR},</if>

    <if test="goodsname != null">goodsname = #{goodsname,jdbcType=VARCHAR},</if>

    </trim>

    </insert>

  • 执行测试语句前的数据如下图:(注意看 id=14 的行,测试语句会修改该行)

【java】Mybatis 存在则修改,没有则新增/插入

  • 使用 Postman 测试 post 请求如下图:

【java】Mybatis 存在则修改,没有则新增/插入

看上图,用来测试的 json 对象是:{"id":14,"mainid":"1","ordercode":"chanchaw01","goodsname":null}
那么根据 Mybatis 的配置,会修改 id=14 的行的字段 ordercodechanchaw01,但是不会修改字段 goodsname ,不过请看执行后的结果:
【java】Mybatis 存在则修改,没有则新增/插入

结果和预期不一致,为什么字段 goodsname 被修改了呢??

  • Java 中打印出了执行SQL

    JDBC Connection [[email protected]] will not be managed by Spring

    ==> Preparing: insert INTO orderdetail ( id, mainid, ordercode ) values ( ?, ?, ? ) ON DUPLICATE KEY UPDATE mainid = ?, ordercode = ?, goodsname = ?

    ==> Parameters: 14(Integer), 1(Integer), chanchaw03(String), 1(Integer), chanchaw03(String), null

    <== Updates: 2

    Closing non transactional SqlSession [[email protected]]

    Creating a new SqlSession

    SqlSession [[email protected]] was not registered for synchronization because synchronization is not active

    JDBC Connection [[email protected]] will not be managed by Spring

    ==> Preparing: insert INTO orderdetail ( id, mainid, ordercode ) values ( ?, ?, ? ) ON DUPLICATE KEY UPDATE mainid = ?, ordercode = ?, goodsname = ?

    ==> Parameters: 14(Integer), 1(Integer), chanchaw01(String), 1(Integer), chanchaw01(String), null

    <== Updates: 2

    Closing non transactional SqlSession [[email protected]]

    看上面 console 打印的 SQL 语句,inser into 部分判断到 goodsnamenull ,所以没有拼接该字段最终形成 insert INTO orderdetail ( id, mainid, ordercode ) values ( ?, ?, ? ) ,但是后面的 UPDATE 没有检测到 null ,导致形成了 SQLUPDATE mainid = ?, ordercode = ?, goodsname = ? 最终字段 goodsname 被修改为了 null,这是什么问题?是我 xml 文件中写的不对?

  • 有必要的话:QQ = 4092223171

回答

你试下

ON DUPLICATE KEY UPDATE

<trim suffixOverrides=",">

<if test="mainid != null">mainid = VALUES(mainid),</if>

<if test="ordercode != null">ordercode = VALUES(ordercode),</if>

<if test="goodsname != null">goodsname = VALUES(goodsname),</if>

</trim>

以上是 【java】Mybatis 存在则修改,没有则新增/插入 的全部内容, 来源链接: utcz.com/a/74509.html

回到顶部