mybatis oracle mysql 批量插入

[size=large]一、oracle的批量插入方式[/size]

insert into db(id, zgbh, shbzh)
select '1', '2', '3' from dual
union all select '2', '3', '4' from dual
union all select '3', '4', '5' from dual
union all select '4', '5', '6' from dual
union all select '5', '6', '7' from dual


<insert id="insertMoSmsList" parameterType="com.xxx.XxxBean">
INSERT INTO TBL_xxx_DETAIL
(
id, zgbh, shbzh, ReceiveTime
) SELECT SEQ_xxx_DETAIL.NEXTVAL, A.* FROM(
<foreach collection="list" item="item" index="index" separator="UNION ALL">
<![CDATA[
SELECT
#{item.id, jdbcType=INTEGER} AS id,
#{item.zgbh, jdbcType=VARCHAR} AS zgbh,
#{item.shbzh, jdbcType=VARCHAR} AS shbzh,
TO_DATE(#{item.receiveTime, jdbcType=DATE},'yyyy-mm-dd hh24:mi:ss') AS ReceiveTime
FROM dual
]]>
</foreach>
) A
</insert>


[size=large]二、mysql的批量插入方式[/size]

INSERT INTO MyTable(ID,NAME) VALUES
(7,'003'),(8,'004'),(9,'005')


<insert id="insertBatch" >
insert into student ( NAME,SEX,ADDRESS,TELEPHONE,TID)
values
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item.name},
#{item.sex},
#{item.address},
#{item.telephone},
#{item.tId}
</foreach>
</insert>


[size=large]三、容易发生的异常[/size]
1. "A.*" 无效列
[quote]ORA-00918:未明确定义列[/quote]
解决方法:
#{item.senderPhone, jdbcType=VARCHAR} [color=red]AS SenderPhone[/color]
如果不设置列名,那么#{item.senderPhone}的值就是默认列名,那么就有很多概率会产生列名重复,而产生异常。(两个列的值完全可能一样)

2. #{item.receiveTime} 值为null时,必须指定转换类型
[quote]JDBC requires that the JdbcType must be specified for all nullable parameter

MyBatis 插入空值时,需要指定JdbcType
mybatis insert空值报空值异常,但是在pl/sql不会提示错误,主要原因是mybatis无法进行转换[/quote]
解决方法:
因为你传入的参数的字段为null对象无法获取对应的jdbcType类型,而报的错误。
你只要在insert语句中insert的对象加上jdbcType就可以了,修改如下:

#{item.receiveTime, jdbcType=DATE} AS ReceiveTime,


TO_DATE(#{item.receiveTime, jdbcType=DATE},'yyyy-mm-dd hh24:mi:ss') AS ReceiveTime,

这样就可以解决以上错误了。

[quote][url]http://java-xp.iteye.com/blog/1671409[/url][/quote]
[quote][url]http://makemyownlife.iteye.com/blog/1610021[/url][/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值