【问题标题】:How to patch insertSelective into database using Mybatis xml foreach tag如何使用 Mybatis xml foreach 标签将 insertSelective 修补到数据库中
【发布时间】:2020-01-07 06:07:21
【问题描述】:
<insert id="insertSelectiveBatch" parameterType="cn.indweb.blogcore.repository.model.Article">
<!--
  WARNING - @mbg.generated
  This element is automatically generated by MyBatis Generator, do not modify.
  This element was generated on Thu Aug 22 10:38:37 CST 2019.
-->
<foreach item="item" collection="list" open="" close="" separator=";">
insert into tbl_article
<trim prefix="(" suffix=")" suffixOverrides=",">
  <if test="item.id != null">
    id,
  </if>
  <if test="item.title != null">
    title,
  </if>
  <if test="item.author != null">
    author,
  </if>
  <if test="item.content != null">
    content,
  </if>
  <if test="item.createdTime != null">
    created_time,
  </if>
  <if test="item.deletedTime != null">
    deleted_time,
  </if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
  <if test="item.id != null">
    #{item.id,jdbcType=VARCHAR},
  </if>
  <if test="item.title != null">
    #{item.title,jdbcType=VARCHAR},
  </if>
  <if test="item.author != null">
    #{item.author,jdbcType=VARCHAR},
  </if>
  <if test="item.content != null">
    #{item.content,jdbcType=VARCHAR},
  </if>
  <if test="item.createdTime != null">
    #{item.createdTime,jdbcType=TIMESTAMP},
  </if>
  <if test="item.deletedTime != null">
    #{item.deletedTime,jdbcType=TIMESTAMP},
  </if>
</trim>
</foreach>


这是我尝试过的代码,但没有奏效。错误消息是“java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在“插入 tbl_article...”附近使用的正确语法”

【问题讨论】:

    标签: mybatis


    【解决方案1】:

    您的语句提供了一个集合类型的参数。你根本不需要parameterType。请尝试将其删除,然后再次尝试运行。

    【讨论】:

    • 请完整解释您的答案。想象你正在回应一位忙碌的同事。
    • 您的语句提供了一个集合类型的参数。你根本不需要parameterType。请尝试删除它,然后尝试再次运行。
    • 删除 parameterType 无效。我猜 myBatis 无法解析像“INSERT INTO tbl_article (id, title, ...) VALUES (id, title, ...) ; INSERT INTO tbl_article (id, title, ...) VALUES (id, title, . ..)"
    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 2021-04-29
    • 1970-01-01
    • 2015-10-01
    • 2019-05-27
    • 1970-01-01
    相关资源
    最近更新 更多