MyBatis 批量新增 修改

在这里插入图片描述
MyBatis批量新增
传入参数为List 类型
使用数据库为Oracle

mapper接口

void insertIntoYEAR(@Param("list")List<yearReportData> paramList);
  <insert id="insertIntoYEAR"  parameterType="java.util.List">
     
     		insert into	XSR_SIF_TRZ_YEARMONEY_PLAN	
    	   (
    	      ym_id,
    	      lastyear_plan_money,
    	      lastyear_actual_money,
    	      currentyear_plan_money,
    	      note,
    	      year_project_id,
    	      year_report_id,
    	      created_by,
    	      creation_date
    	    
    	   )	
		
			 
			  <foreach collection="list" item="item" index="index" separator="union all">		
			  
			  <trim prefix="select" suffix="from dual" suffixOverrides=",">	
			  <choose>
			  <when test="item.ym_id != null">#{item.ym_id,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose>
			  <choose>
			  <when test="item.lastyear_plan_money != null">#{item.lastyear_plan_money,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose>
			  <choose>
			  <when test="item.lastyear_actual_money != null">#{item.lastyear_actual_money,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose>
			  <choose>
			  <when test="item.currentyear_plan_money != null">#{item.currentyear_plan_money,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose>
			  <choose>
			  <when test="item.note!= null">#{item.note,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose><choose>
			  <when test="item.year_project_id != null">#{item.year_project_id,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose><choose>
			  <when test="item.year_report_id != null">#{item.year_report_id,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose><choose>
			  <when test="item.created_by != null">#{item.created_by,jdbcType=VARCHAR},</when>
			  <otherwise>null,</otherwise>
			  </choose>
			  sysdate,

			</trim>
			  </foreach>
			  
     
     
     </insert>

批量更新

void updateInOrOut(@Param("list")List<monthReportData> paramList);
<update id="updateInOrOut" parameterType="java.util.List">
    		
        <if test="list!=null">
            <foreach collection="list" item="item" index= "index" open="begin" close=";end;" separator=";">
            update XSR_SIF_TRZ_INOROUT
                <set>
                <if test="item.slastmonth_actual_money != null">slastmonth_actual_money=#{item.slastmonth_actual_money,jdbcType=VARCHAR},</if>
				<if test="item.scurrentmonth_plan_money != null">scurrentmonth_plan_money=#{item.scurrentmonth_plan_money,jdbcType=VARCHAR},</if>
				<if test="item.zlastmonth_actual_money != null">zlastmonth_actual_money=#{item.zlastmonth_actual_money,jdbcType=VARCHAR},</if>
				<if test="item.zcurrentmonth_plan_money != null">zcurrentmonth_plan_money=#{item.zcurrentmonth_plan_money,jdbcType=VARCHAR},</if>
				<if test="item.plan_year != null">plan_year=#{item.plan_year,jdbcType=VARCHAR},</if>
				<if test="item.plan_month != null">plan_month=#{item.plan_month,jdbcType=VARCHAR},</if>
				LAST_UPDATE_DATE = sysdate
				</set>
				where 1=1
				<if test="item.report_id != null">and report_id=#{item.report_id}</if>
				<if test="item.outproject_id != null">and outproject_id=#{item.outproject_id}</if>
				</foreach>
        </if>
       
    </update>

在这里插入图片描述

MyBatis中,可以通过批量更新操作来一次性更新多条数据。有两种常见的方式来实现批量更新。 第一种方式是在业务代码中循环遍历逐条更新。这种方式最简单,也最不容易出错。在代码中,可以使用一个循环来遍历数据列表,并逐条调用update方法进行更新。如果更新失败,可以进行一些其他的操作,比如打印出错日志等。这种方式的代码可能类似于下面的示例: ```java updateBatch(List<MyData> datas){ for(MyData data : datas){ try{ myDataDao.update(data);//更新一条数据 } catch(Exception e){ ...//如果更新失败可以做一些其他的操作,比如说打印出错日志等 } } } ``` 在MyBatis的XML文件中,可以使用update语句来实现更新操作。 第二种方式是一次性更新所有数据,即通过一条SQL语句来更新所有数据。这种方式可以在一次数据库连接中更新所有数据,避免了频繁数据库建立和断开连接的开销,从而提高数据更新效率。但是这种方式的问题是如果在更新过程中出错,将很难知道具体是哪个数据出错。通常的做法是进行折中,一次批量更新一部分数据,比如分页进行更新,每次更新一定数量的数据。这样可以分担出错的概率,也更容易定位到出错的位置。 当数据量非常大时,批量更新也可能导致更新效率低下。在这种情况下,可能需要考虑其他方案,比如引入缓存机制等。 总结起来,MyBatis中实现批量修改更新操作可以通过循环遍历逐条更新或一次性更新所有数据的方式来实现。具体选择哪种方式取决于具体需求和数据量大小。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Mybatis中进行批量更新(updateBatch)](https://blog.csdn.net/xyjawq1/article/details/74129316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值