利用MyBatis动态SQL的特性,我们可以做一些批量的操作,本文将介绍MySQL、Oracle SQL方言的批量插入、删除写法,更多详细情况请查看MyBatis官方文档。
批量插入
mysql:
insert into user(username, password) values
separator=",">
(#{item.username},
#{item.password} )
oracle:
insert into user(username, password)
select
#{item.username,jdbcType=VARCHAR},
#{item.password,jdbcType=VARCHAR}
from dual
oracle使用 sequence主键策略
insert into user(id, username, password)
select SEQ_USER_ID.NEXTVAL,T.* from(
separator="UNION">
SELECT
#{item.username,jdbcType=VARCHAR},
#{item.password,jdbcType=VARCHAR}
from dual
) T
批量删除
delete from user
where id in
#{id}