方式一:
1 2 3 4 5 6 7 8 9 10 | < update id = "updateBatch" parameterType = "java.util.List" > < foreach collection = "list" item = "item" index = "index" open = "" close = "" separator = ";" > update tableName < set > name=${item.name}, name2=${item.name2} </ set > where id = ${item.id} </ foreach > </ update > |
但Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行。所以需要在连接mysql的url上加 &allowMultiQueries=true 这个才可以执行。
方式二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < update id = "updateBatch" parameterType = "java.util.List" > update tableName < trim prefix = "set" suffixOverrides = "," > < trim prefix = "c_name =case" suffix = "end," > < foreach collection = "list" item = "cus" > < if test = "cus.name!=null" > when id=#{cus.id} then #{cus.name} </ if > </ foreach > </ trim > < trim prefix = "c_age =case" suffix = "end," > < foreach collection = "list" item = "cus" > < if test = "cus.age!=null" > when id=#{cus.id} then #{cus.age} </ if > </ foreach > </ trim > </ trim > < where > < foreach collection = "list" separator = "or" item = "cus" > id = #{cus.id} </ foreach > </ where > </ update > |
这种方式貌似效率不高,但是可以实现,而且不用改动mysql连接