mybatis 批量插入oracle

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>


發佈了72 篇原創文章 · 獲贊 51 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章