MyBatis的批量操作

oracle中 形如 update *** set *** where ** in(....) 这种语句 in所在的集合有条数限制 为1000条

在工作中遇见了好多需要执行批量操作的功能,也在网上找了好多的例子,感觉有些散乱,就自己整理了一遍;

一、批量添加

关于批量添加MySql 和 Oracle的语句是不一样的,首先介绍oracle

<insert id="saveBatch" parameterType="java.util.List">
		INSERT INTO TABLE_NAME (id ,NEWS_ID ,COVERPHOTO_URL)
		SELECT ss.*
		from (
		<foreach collection="list" item="item" index="index" separator="UNION ALL" >
			SELECT 
			   #{item.id}
				,#{item.newsId} 
				,#{item.coverPhotoUrl} 
				,#{item.readingQuantity} 
			FROM DUAL
		</foreach>
		) ss
</insert>

或者是这种

<insert id="saveBatch" parameterType="java.util.List">
		INSERT INTO TABLE_NAME (id ,NEWS_ID ,COVERPHOTO_URL)
		<foreach collection="list" item="item" index="index" separator="union all" > 
			(SELECT 
			   #{item.id}
				,#{item.newsId} 
				,#{item.coverPhotoUrl} 
				,#{item.readingQuantity} 
			FROM DUAL)
		</foreach>
</insert>

要注意的一点是list不能为空,不然会报异常,说是缺少values

上面的ID没有使用自增序列,下面的是使用自增序列的

<insert id="saveBatch" parameterType="java.util.List">
		INSERT INTO TABLE_NAME (id , NAME,AREA,ADDRESS,DESCRIBE)
		SELECT SERVICE_P_MSG_ID_SEQ.NEXTVAL , ss.*
		from (
		<foreach collection="list" item="item" index="index" separator="UNION ALL" >
			SELECT
			#{item.name, jdbcType=VARCHAR}
			,#{item.area,jdbcType=VARCHAR}
			,#{item.address,jdbcType=VARCHAR}
			from dual
		</foreach>
		) ss
</insert>

下面是MySql

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
        INSERT INTO USER (NAME, AGE,DEPT_CODE) VALUES  
        <foreach collection="list" index="index" item="item" open="" close="" separator=",">  
            (#{item.name} 
			 ,#{item.age} 
             ,#{item.deptCode}  
            )  
        </foreach>  
</insert>
这种写法的的效率比把 INSERT 放到<foreach>标签中的写法高一些,但是要注意list.size()不宜过大,这个量我也没测试过,用的时候自己把握就好。

下面展示一下 INSERT 在<foreach>标签中的写法,因为MySql是支持自增主键的,所以写起来比Oracle那种简单点

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            insert into user (name, age,dept_code) values  
            (#{item.name,jdbcType=VARCHAR},  
            #{item.age,jdbcType=INTEGER},  
             #{item.deptCode,jdbcType=VARCHAR}  
            )  
        </foreach>  
</insert>  

二、批量修改

批量修改也是有2中写法,根据个人喜好

<update id="batchUpdateStudentWithMap" parameterType="Map" >  
    UPDATE STUDENT SET name = #{name} WHERE id IN   
    <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">   
        #{item}   
    </foreach>  
</update>

第二种是把语句都写在<froeach>标签中

<update id="updateBatch" parameterType="="java.util.List">
    <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";">
       update TABLE_NAME t
            <set>
               test=${item.test}+1
            </set>
       where t.NEWS_ID = ${item.newsId}
    </foreach>
</update>


三、批量删除

<delete id="batchDeleteStudent" parameterType="List">  
    DELETE FROM TABLE_NAME WHERE id IN  
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">   
        #{item}   
    </foreach>  
</delete>
oracle中 形如 update *** set *** where ** in(....) 这种语句 in所在的集合有条数限制 为1000条

这算是首位呼应吧,批量操作时一定要注意数据的量!!!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值