首页 > 技术文章 > Mybatis---批量处理

wffzk 2021-12-27 18:12 原文

在项目当中我们总会遇到这种情况,一次查询满足不了页面所需展示的字段。这时候需要根据查出来的list循环去查询另外的字段,有人会在循环中执行数据库操作,这样会建立多次数据库连接,不但耗费性能而且会导致连接数满。尤其是查询大数据量的时候,性能测试的时差体现的很明显。我们应当避免这样的操作,去用批量处理。

说明:item集合或数组里的元素(对象)

        collection集合类型(数组或集合)

     open以什么开始

     close以什么结束

     separator中间以什么相连

1.批查询 select、

单参数 可以用 IN 也可以用多参数模式

   <!-- 查询日志的操作   -->
    <select id="selectJournalTime" resultType="cn.loan.vo.biz.JournalTimeVo" parameterType="java.util.List" >
      	   select
            DATE_FORMAT(acc.update_time,'%Y-%m-%d %H:%i:%S') as updateTime,
            DATE_FORMAT(acc.redemptionTime,'%Y-%m-%d %H:%i:%S') as redemptionTime,
            acc.auditSurfaceId as auditSurfaceId
            from auditJournalMinute acc
            where
            acc.auditSurfaceId in
	 <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>	
    </select>

多参数 用UNION ALL 注意括号不要漏

   <!-- 查询日志的操作   -->
    <select id="selectJournalTime" resultType="cn.loan.vo.biz.JournalTimeVo" parameterType="java.util.List" >
        <foreach collection="list" item="item" index="index" separator="UNION ALL" >
            (select
            DATE_FORMAT(acc.update_time,'%Y-%m-%d %H:%i:%S') as updateTime,
            DATE_FORMAT(acc.redemptionTime,'%Y-%m-%d %H:%i:%S') as redemptionTime,
            acc.auditSurfaceId as auditSurfaceId
            from auditJournalMinute acc
            where
            acc.auditSurfaceId=#{item.id}
            and acc.`status`=#{item.status}
            order by acc.update_time asc
            limit 1)
        </foreach>
    </select>

2.批量插入 insert 

<insert id="insertAuditHistory" parameterType="java.util.List">
    insert into audit_history
    (backgroundUserId,
    show_time,
    commit_num,
    lend_num)
    values
    <foreach collection="list" item="item" separator=",">
        (#{item.backgrounduserid},#{item.showTime},#{item.commitNum},#{item.lendNum})
    </foreach>
</insert>

3.批量删除 delete

<delete id="deleteArtworkMaster" parameterType="java.util.List">
   delete from 
     artworkMasterPhotoAlbum
  where 
	artworkMasterPhotoAlbumId IN  
        <foreach collection="list" item="item" index="index" open="("  close=")" separator="," >  
	 #{item}  
	</foreach>  
</delete>

4.批量更新

<update id="updateBatch" parameterType="java.util.List">
        update role
        set  update_time=
        <foreach collection="list" item="item" index="index"
                 separator=" " open="case roleId" close="end">
            when #{item.id} then #{item.date}
        </foreach>
        where roleId in
        <foreach collection="list" index="index" item="item"
                 separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

多字段多条件更新 需要给mysql配置批量执行,在spring.datasource.url后加上allowMultiQueries=true
例如:spring.datasource.url=jdbc:mysql://127.0.0.1:3306/secondleaseback?allowMultiQueries=true

<update id="updateBatch" parameterType="java.util.List">
	 <foreach collection="list" item="item" index="index" separator=";" open="" close="">
            update role
            <set>
                update_time=#{item.date},
                create_time=#{item.date}
            </set>
            where roleId=#{item.id}
         </foreach>
 </update>

注: insert 的时候 如果需要返回主键,在 <insert>标签中增加 useGeneratedKeys=“true” keyProperty=“实体主键id字段"

推荐阅读