Mybatis---批量处理
在项目当中我们总会遇到这种情况,一次查询满足不了页面所需展示的字段。这时候需要根据查出来的list循环去查询另外的字段,有人会在循环中执行数据库操作,这样会建立多次数据库连接,不但耗费性能而且会导致连接数满。尤其是查询大数据量的时候,性能测试的时差体现的很明显。我们应当避免这样的操作,去用批量处理。
说明:item集合或数组里的元素(对象)
collection集合类型(数组或集合)
open以什么开始
close以什么结束
separator中间以什么相连
1.批查询 select、
单参数 可以用 IN 也可以用多参数模式
多参数 用UNION ALL 注意括号不要漏
2.批量插入 insert
insert into audit_history (backgroundUserId, show_time, commit_num, lend_num) values (#{item.backgrounduserid},#{item.showTime},#{item.commitNum},#{item.lendNum})
3.批量删除 delete
delete from artworkMasterPhotoAlbum where artworkMasterPhotoAlbumId IN #{item}
4.批量更新
update role set update_time= when #{item.id} then #{item.date} where roleId in#{item.id}
多字段多条件更新 需要给mysql配置批量执行,在spring.datasource.url后加上allowMultiQueries=true
例如:spring.datasource.url=jdbc:mysql://127.0.0.1:3306/secondleaseback?allowMultiQueries=true
update role update_time=#{item.date}, create_time=#{item.date} where roleId=#{item.id}
注: insert 的时候 如果需要返回主键,在
标签中增加 useGeneratedKeys=“true” keyProperty=“实体主键id字段"