一.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,734评论 0 4
  • 这些属性是否生效取决于对应的组件是否声明为 Spring 应用程序上下文里的 Bean(基本是自动配置的),为一个...
    发光的鱼阅读 1,458评论 0 14
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
    Joyyx阅读 8,359评论 0 16
  • 20170904—20170914 不是故乡,胜似故乡;缘于亲人,爱此一方。 海拉尔—天津—青岛 绿意盎然 莱州湾...
    刘宝琦阅读 358评论 0 0