关键是mapper.xml的SQL语句映射配置
批量保存(一条SQL)
批量插入的SQL语句:
insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo)
useGeneratedKeys="true">
SELECT
LAST_INSERT_ID()
insert into wd_solr
(fayu_id, tablename,
name,logo,description,section_no,look_count,favorite_count,create_uid,create_time,update_time,timestamp)
values
separator=",">
(
#{wdSolr.fayuId},#{wdSolr.tablename},#{wdSolr.name},#{wdSolr.logo},
#{wdSolr.description},#{wdSolr.sectionNo},#{wdSolr.lookCount},#{wdSolr.favoriteCount},
#{wdSolr.createUid},#{wdSolr.createTime},#{wdSolr.updateTime},#{wdSolr.timestamp}
)
批量更新(一条SQL)
update orders
when id=#{i.id} then #{i.userId}
when id=#{i.id} then #{i.number}
when id=#{i.id} then #{i.createtime}
when id=#{i.id} then #{i.note}
where
id=#{i.id}
一条记录update一次,性能比较差,容易造成阻塞。
MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。
UPDATE course
SET name = CASE id
WHEN 1 THEN 'name1'
WHEN 2 THEN 'name2'
WHEN 3 THEN 'name3'
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)