MyBatis批量保存(Oracle)
oracle 批量插入与mysql 的批量插入的方式不同
insert into tablename()values(),(),(); ---这个是mysql 的批量插入形式
insert all into tablename() values() into tablename() values() -------这个是Oracle批量插入形式
你可能遇到的错误:
- 序列不存在=========> 没有创建与脚本中一致的序列号
- 表格或者视图不存在(Cause: java.sql.SQLSyntaxErrorException: ORA-00942: 表格或視觀表不存在) =========> 数据库中的table name与脚本中的不一致
- 命令未正确结束(ORA-00933: SQL 命令未正确结束 / ORA-00933: SQL command not properly ended ;) =========> mapper或者注解中SQL脚本不正确!
- 字段类型对不上
- 缺少字段
- ...
示例批量插入(xml、注解)
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.**.IOrderMapper">
<resultMap id="Order" type="com.mdb.cips.remittance.pojo.entity.CipsOrder">
</resultMap>
<insert id="saveBatch" parameterType="com.**.Order" useGeneratedKeys="false">
insert into TB_ORDER (
order_id, order_no, booking_id, cif_no, bank_name_from, bank_name_to, bank_account_from, bank_account_to,
currency_from, currency_to, order_type, amount_to, payee_username, amount_from, amount_total_from, status, remit_fee,
remit_fee_currency)
select SEQ_ORDER.NEXTVAL, A.* from(
<foreach collection='list' item='item' index='index' separator='union all'>
select
#{item.orderNo}, #{item.bookingId}, #{item.cifNo}, #{item.bankNameFrom},
#{item.bankNameTo}, #{item.bankAccountFrom}, #{item.bankAccountTo}, #{item.currencyFrom},
#{item.currencyTo}, #{item.orderType}, #{item.amountTo},#{item.payeeUsername},
#{item.amountFrom},#{item.amountTotalFrom},#{item.status},#{item.remitFee}, #{item.remitFeeCurrency}
from dual
</foreach>) A
</insert>
</mapper>
注解(Update)
@Insert("<script>" +
"insert into TB_ORDER (order_id, order_no, booking_id, cif_no, bank_name_from, bank_name_to, bank_account_from, bank_account_to, currency_from, currency_to, order_type, amount_to, amount_from, amount_total_from, status, remit_fee, remit_fee_currency) select SEQ_ORDER.NEXTVAL, A.* from(" +
"<foreach collection='list' item='item' separator='union all'> " +
"select #{item.orderNo}, #{item.bookingId}, #{item.cifNo}, #{item.bankNameFrom}, #{item.bankNameTo}, #{item.bankAccountFrom}, #{item.bankAccountTo}, #{item.currencyFrom}, #{item.currencyTo}, #{item.orderType}, #{item.amountTo}, #{item.amountFrom},#{item.amountTotalFrom},#{item.status},#{item.remitFee}, #{item.remitFeeCurrency} from dual" +
"</foreach>) A" +
"</script>")
int saveBatch(@Param("list") List<Order> list);
控制台打印SQL
insert into TB_ORDER (order_id, order_no, booking_id, cif_no, bank_name_from, bank_name_to, bank_account_from,
bank_account_to, currency_from, currency_to, order_type, amount_to, payee_username,
amount_from, amount_total_from, status, remit_fee, remit_fee_currency)
select SEQ_ORDER.NEXTVAL, A.*
from (select ?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
from dual
union all
select ?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
from dual
union all
select ?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
from dual
union all
select ?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
from dual) A
注意:
MyBatis批量插入时,组装SQL过长会有问题,一定要根据批量插入数据量进行切割,再批次提交保存!!!
MyBatis批量插入时,组装SQL过长会有问题,一定要根据批量插入数据量进行切割,再批次提交保存!!!
MyBatis批量插入时,组装SQL过长会有问题,一定要根据批量插入数据量进行切割,再批次提交保存!!!