First, MySQL requires a database connection configuration &allowmultiqueries=true
Jdbc:mysql://127.0.0.1:3306/mybank?useunicode=true&characterencoding=utf8&allowmultiqueries=true
Oracle supports execution of multiple statements under the following 3 identical
<update id= "BatchUpdate" parametertype= "java.util.List" > <foreach collection= "List" item= "Item" index= "Index" open= "Begin" close= "END;" separator= ";" > update t_emp_1 <set> Age = #{item.age}+1,name =#{item.name} </set> where id = #{item.id} </foreach> </update>
<update id= "BatchUpdate" parametertype= "java.util.List" > <foreach collection= "List" item= "Item" index= "Index" open= "Begin" close= "END;" separator= "> update t_emp_1 <set> Age = #{item.age}+1,name=# {Item.name} </set> where id = #{item.id}; </foreach> </update>
<update id= "BatchUpdate" parametertype= "java.util.List" > begin <foreach collection= "List" item= "Item" index= "index" separator= "" > update t_emp_1 <set> Age = #{item.age}+1,name=#{ Item.name} </set> where id = #{item.id}; </foreach> end; </update>
The main use of foreach is in the build in condition, which can iterate a collection in an SQL statement. The properties of the Foreach element are mainly item,index,collection,open,separator,close. The item represents the alias of each element in the collection when it iterates, and index specifies a name that represents the position at which each iteration occurs during the iteration, and open indicates what the statement begins with, and separator indicates what symbol is used as the delimiter between each iteration. Close means the end, the most critical and error-prone when using foreach is the collection property, which must be specified, but in different cases the value of the property is not the same, there are 3 main cases:
1. If a single parameter is passed in and the parameter type is a list, the collection property value is List
2. If a single parameter is passed in and the parameter type is an array, the value of the collection property is array
3. If the parameters passed in are multiple, we need to encapsulate them into a map, of course, the single parameter can also be encapsulated as a map, in fact, if you pass in the parameter, in the breast will also wrap it into a map, the map key is the parameter name, So this time the collection property value is the key to the incoming list or array object in its own encapsulated map
Bulk Delete
<delete id= "batchdeletestudent" parametertype= "List" > delete from STUDENT WHERE ID in <foreach collection= "list" index= "index" item= "Item" open= "(" separator= "," close= ")" > #{item} </foreach> </delete>
Bulk Update Note: Oracle in the form of UPDATE * * * * * * * * where * in (...) this statement in the collection has a limit of 1000 bars
<update id= "batchupdatestudent" parametertype= "List" > update STUDENT SET name = "5566" WHERE ID in < foreach collection= "list" item= "item" index= "Index" open= "(" separator= "," close= ")" > #{item} </ Foreach> </update>
<update id= "Batchupdatestudentwithmap" parametertype= "Map" > update STUDENT SET name = #{name} WHERE ID in <foreach collection= "idlist" index= "index" item= "Item" open= "(" separator= "," close= ")" > #{item} </ Foreach> </update>
Bulk INSERT MySQL is not the same as Oracle
Mysql:<insert id= "batchinsertstudent" parametertype= "java.util.List" > insert INTO STUDENT (Id,name,sex, tel,address) VALUES <foreach collection= "list" item= "item" index= "index" separator= "," > (#{ Item.id},#{item.name},#{item.sex},#{item.tel},#{item.address}) </foreach>
Oracle:<insert id= "insertbatch4oracle" parametertype= "List" > INSERT INTO AA (A, b) <foreach collection= "List" item= "item" index= "index" separator= "union All" > select #{item.a},#{item.b} from dual </foreach> </insert>
MyBatis Bulk Operations