mybatis 批量插入oracle
使用如下語句
<insert id="addShortMessage" parameterType="list" useGeneratedKeys="false">
INSERT INTO dm_short_message_tb(ID,NAME,PHONE,CONTENT,CONTENT_TYPE,STATUS)
select cd.* from(
<foreach collection="list" item="item" index="index" close=")" open="(" separator="union">
select
#{item.id,jdbcType=VARCHAR},#{item.name,jdbcType=VARCHAR},
#{item.phone,jdbcType=VARCHAR},#{item.content,jdbcType=VARCHAR},
#{item.contentType,jdbcType=VARCHAR},#{item.status,jdbcType=VARCHAR}
from dual
</foreach>
) cd
</insert>
網上的大部分錯誤由於 沒有指定useGeneratedKeys="false"
dao層代碼格式如下
public interface SendMessageDao {
public void addShortMessage(List<ShortMessage> listMessage);
}
mybatis 批量插入mysql 如下
<insert id="addShortMessage" parameterType="list">
insert into dm_short_message_tb(ID,NAME,PHONE,CONTENT,CONTENT_TYPE,STATUS)
values
<foreach collection="list"item="obj" index="index"separator="," >
(#{item.id,jdbcType=VARCHAR},#{item.name,jdbcType=VARCHAR},
#{item.phone,jdbcType=VARCHAR},#{item.content,jdbcType=VARCHAR},
#{item.contentType,jdbcType=VARCHAR},#{item.status,jdbcType=VARCHAR})
</foreach>
</insert>