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,

posted on 2017-11-17 15:16  傲视苍穹  阅读(736)  评论(0编辑  收藏  举报