phaneven
phaneven

Reputation: 1027

How to patch insertSelective into database using Mybatis xml foreach tag

<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>


This is the code I have tried, but not worked. Error message was "java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into tbl_article...'"

Upvotes: 0

Views: 306

Answers (1)

Kaikang Sun
Kaikang Sun

Reputation: 1

Your statement is provided a parameter of Collection type. You don't need parameterType at all. Please try to remove it and subsequently try run again.

Upvotes: -1

Related Questions