一、mybatis批量修改
1.1 根据id批量更新
update
${dbName}.tableName tn
set
when #{item.id} then #{item.columnOne}
when #{item.id} then #{item.columnTwo}
when #{item.id} then #{item.columnThree}
when #{item.id} then #{item.columnFour}
when #{item.id} then #{item.columnFive}
when #{item.id} then #{item.columnSix}
when #{item.id} then #{item.columnSeven}
when #{item.id} then #{item.columnEight}
when #{item.id} then #{item.columnNine}
, tn.status = '1'
where
tn.id in
#{item.id}
1.2 根据多个字段更新其他字段值(批量)
1.2.1写法一
UPDATE ${dbName}.tableName
SET column_one =
when column_two = #{item.columnTwo} and column_three = #{item.columnThree} and column_four = #{item.columnFour}
and column_five = #{item.columnFive}
then #{item.columnOne}
,column_six =
when column_two = #{item.columnTwo} and column_three = #{item.columnThree} and column_four = #{item.columnFour}
and column_five = #{item.columnFive}
then #{item.columnSix}
WHERE (column_two,column_three,column_four,column_five) IN
( #{item.columnTwo},#{item.columnThree},#{item.columnFour},#{item.columnFive})
1.2.2写法二
UPDATE ${dbName}.tableName
column_one = #{item.columnOne},
column_two = #{item.columnTwo},
WHERE column_three = #{columnThree} and column_four = #{columnFour} and column_five = #{columnFive} and column_six = #{columnSix}
1.2.3说明
1.关于写法二:Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行。所以需要在连接mysql的url上加 &allowMultiQueries=true 这个才可以执行
2.更新记录数不大的情况下,写法二的性能高于写法一;测试修改1500条数据,写法二耗时500ms,写法一耗时2106ms;
3.实际开发个人使用的是第一种写法,原因暂时为项目组开发要求。
浙公网安备 33010602011771号