mybatis oracle mysql 批量插入
一、oracle的批量插入方式
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>
二、mysql的批量插入方式
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>

三、容易发生的异常(坑爹)
1. "A.*" 无效列
引用 ORA-00918:未明确定义列
解决方法:
#{item.senderPhone, jdbcType=VARCHAR} AS SenderPhone
如果不设置列名,那么#{item.senderPhone}的值就是默认列名,那么就有很多概率会产生列名重复,而产生异常。(两个列的值完全可能一样)
2. #{item.receiveTime} 值为null时,必须指定转换类型
引用 JDBC requires that the JdbcType must be specified for all nullable parameter
MyBatis 插入空值时,需要指定JdbcType
mybatis insert空值报空值异常,但是在pl/sql不会提示错误,主要原因是mybatis无法进行转换;
解决方法:
因为你传入的参数的字段为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,

最新文章

  1. handlebars.js的运用与整理
  2. angularjs自带过滤器
  3. 执行时关闭标识位 FD_CLOEXEC 的作用
  4. 获取Ip地址
  5. COM中的REFIID小解【转】
  6. codevs1322 单词矩阵
  7. 使用mii-tool设置网卡速率
  8. opencv 在工业中的应用:圆孔定位
  9. 《C和指针》章节后编程练习解答参考——第9章
  10. hdoj 1789 Doing Homework again
  11. ASP.NET中生成rss.xml
  12. Ado.net 类扩展属性
  13. pythonllk
  14. Entity Framework Core 关联删除
  15. [总结]jQuery之常用函数方法参考手册
  16. 04-MySQL的存储引擎和列的常用类型
  17. 爬虫之urllib.request基础使用(一)
  18. 译: 2. RabbitMQ Spring AMQP 之 Work Queues
  19. hdu 2519 求组合数
  20. ios app: 使用企业license设置发布app的过程

热门文章

  1. CSS3总结七:变换(transform)
  2. count(*),count(1),count(字段)
  3. Vue-router 报NavigationDuplicated的可能解决方案
  4. linux命令详解——top
  5. PHP提取富文本字符串中的纯文本,并进行进行截取
  6. zabbix 自定义Key (六)
  7. css3正方体
  8. ios 打包下
  9. docker run always
  10. 08-sp_who2和inputbuffer的使用,连接数