mybatis foreach Batch insert data: A distinction between Oracle and MySQL

  • 2021-01-03 20:53:00
  • OfStack

Here is an introduction to mybatis foreach batch insert data: Differences between Oracle and MySQL:

The & # 8226; The main difference lies in the setting of the separator attribute in the foreach tag:

The & # 8226; When separator is set to ",", the final spliced code form is: insert into table_name (a,b,c) values (v1,v2,v3) ,(v4,v5,v6) ,...

The & # 8226; When separator is set as "union all", the final spliced code form is: insert into table_name (a,b,c) values (v1,v2,v3) union all (v4,v5,v6) union all...

The & # 8226; See the sample code for details:

Oracle:


<insert id="inserData" parameterType="com.test.aaa.Bac">
  insert into table_name (name, adress, age)
    values
    <foreach collection="list" item="item" index="index" separator="union all">
      (select #{item.name}, 
          #{item.adress}, 
          #{item.age} 
        from dual  )
    <foreach>
</insert>

MySQL:


<insert id="inserData" parameterType="com.test.aaa.Bac">
  insert into table_name (name, adress, age)
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (  #{item.name}, #{item.adress}, #{item.age} )
    <foreach>
</insert>

conclusion

Above is the site to you to introduce mybatis foreach batch insert data: Oracle and MySQL difference, I hope to help you, if you have any questions welcome to leave a message, this site will reply you in time!


Related articles: