一:Oracle数据库
1:批量插入
insert into RECIPEDETAIL (RDID, ROID, TYPE,
NAME, MEDIWEIGHT, MEDINUM,
MONEY, OPERATETIME, CHARGENUMBER,
REMARK, INSTRUCTION, AMOUNTEVERY,
MEDIUNIT, STATUS)
select SEQ_recipeDetail.NEXTVAL,A.* from(
select #{item.roid,jdbcType=DECIMAL}, #{item.type,jdbcType=DECIMAL},
#{item.name,jdbcType=NVARCHAR}, #{item.mediweight,jdbcType=DECIMAL}, #{item.medinum,jdbcType=DECIMAL},
#{item.money,jdbcType=DECIMAL}, #{item.operatetime,jdbcType=TIMESTAMP}, #{item.chargenumber,jdbcType=NVARCHAR},
#{item.remark,jdbcType=NVARCHAR}, #{item.instruction,jdbcType=NVARCHAR}, #{item.amountevery,jdbcType=DECIMAL},
#{item.mediunit,jdbcType=NVARCHAR}, #{item.status,jdbcType=DECIMAL}
FROM DUAL
) A
2:批量更新
update RECIPEDETAIL set
ROID = #{item.roid,jdbcType=DECIMAL},
TYPE = #{item.type,jdbcType=DECIMAL},
NAME = #{item.name,jdbcType=NVARCHAR},
MEDIWEIGHT = #{item.mediweight,jdbcType=DECIMAL},
MEDINUM = #{item.medinum,jdbcType=DECIMAL},
MONEY = #{item.money,jdbcType=DECIMAL},
OPERATETIME = #{item.operatetime,jdbcType=TIMESTAMP},
CHARGENUMBER = #{item.chargenumber,jdbcType=NVARCHAR},
REMARK = #{item.remark,jdbcType=NVARCHAR},
INSTRUCTION = #{item.instruction,jdbcType=NVARCHAR},
AMOUNTEVERY = #{item.amountevery,jdbcType=DECIMAL},
MEDIUNIT = #{item.mediunit,jdbcType=NVARCHAR},
STATUS = #{item.status,jdbcType=DECIMAL}
where RDID = #{item.rdid,jdbcType=DECIMAL}
二:Mysql数据库
批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已。MySql批量操作需要数据库连接配置allowMultiQueries=true才可以。
(1)批量插入
insert into user (name, age,dept_code) values (#{item.name,jdbcType=VARCHAR}, #{item.age,jdbcType=INTEGER}, #{item.deptCode,jdbcType=VARCHAR} )
insert into user (name, age,dept_code) values (#{item.name,jdbcType=VARCHAR}, #{item.age,jdbcType=INTEGER}, #{item.deptCode,jdbcType=VARCHAR} )
update user set name=#{item.name,jdbcType=VARCHAR},age=#{item.age,jdbcType=INTEGER} where id=#{item.id,jdbcType=INTEGER}
delete from user where id=#{item.id,jdbcType=INTEGER}