一.mysql批量操作

一.配置

  • 批量操作jdbc url需添加allowMultiQueries=true屬性:
    實例:spring.datasource.url=jdbc:mysql:[url]?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&allowMultiQueries=true

二.批量增

mybatis

<insert id="" parameterType="java.util.List">
   insert into hello_words (id, value)
    values 
    <foreach collection="list" item="item" index="index" separator="," open="("  close=")" >
      #{item.wordNo},#{item.value}
    </foreach>
</insert>

mysql執行語句

 insert into hello_words (id, value)  insert into hello_words (id, value)  values  (1,2),{2,3)

三.批量更新

mybatis單屬性更新

<update id=""  parameterType="java.util.List">
    UPDATE  hello_words
    SET value=#{value}
    WHERE id IN
    <foreach collection="list" index="index" item="item"  separator="," open="(" close=")">
        #{item.id}
    </foreach>
</update>

mysql單屬性更新

UPDATE  hello_words SET value=#{value} WHERE id IN(1,2)

mybatis多屬性更新

<update id="" parameterType="java.util.List">
    update hello_words
    <trim prefix="set" suffixOverrides=",">
            value1 =
            <foreach collection="list" index="index" item="item"  open="case ID" close="end" separator="" >
                when #{item.id} then #{item.value}
            </foreach>
            ,
            value2=
            <foreach collection="list" index="index" item="item"  open="case ID" close="end" separator="" >
                when #{item.id} then #{item.value2}
            </foreach>
    
    </trim>
    WHERE id IN
    <foreach collection="list" index="index" item="item"  separator="," open="(" close=")">
        #{item.id}
    </foreach>
</update>

mysql多屬性更新

update hello_words
SET value=
case when id =1 then 2 end
case when id =2 then 2 end
where id in(1,2)

四.批量刪除

<delete id="">
    delete from hello_words
    where id in
    <foreach item="item" collection="list" open="(" separator="," close=")">
        #{item.id}
    </foreach>
</delete>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的...
    笨鳥慢飛閱讀 5,605評論 0 4
  • 這些屬性是否生效取決于對應的組件是否聲明為 Spring 應用程序上下文里的 Bean(基本是自動配置的),為一個...
    發光的魚閱讀 1,446評論 0 14
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
    Joyyx閱讀 8,345評論 0 16
  • 20170904—20170914 不是故鄉,勝似故鄉;緣于親人,愛此一方。 海拉爾—天津—青島 綠意盎然 萊州灣...
    劉寶琦閱讀 355評論 0 0