mybatis 的批量更新和批量插入(清晰明了)

在这里插入图片描述

mybatis批量插入

<!-- 批量插入 -->
	<insert id ="insertFenoAppRHHardDeviceList" parameterType="java.util.List">
		<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">  
	          SELECT LAST_INSERT_ID() AS ID    
	    </selectKey>
	INSERT INTO rp_feno_app_rh_hard_device
		(CREATE_DATE ,MODIFY_DATE ,IMEI ,IS_UPGRADE ,FENO_APP_RH_HARD_ID ,IS_DEFAULT)
	VALUES
		<foreach collection ="list" item="item" index= "index" separator =",">
		     (
			  NOW()
			 ,NOW()
		     ,#{
    
    item.imei,jdbcType=VARCHAR}
		     ,#{
    
    item.isUpgrade,jdbcType=BOOLEAN}
		     ,#{
    
    item.fenoAppRHHard.id,jdbcType=BIGINT}
		     ,#{
    
    item.isDefault,jdbcType=BOOLEAN}
		     )
		</foreach>
	</insert>

mybatis批量更新

方法一(推荐)

在mybatis的xml文件中,使用foreach动态标签拼接SQL语句,每一条数据的更新语句对应一条update语句,多条语句最终使用";"号进行拼接。

<update id="updateFenoAppRhHardDeviceList"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update rp_feno_app_rh_hard_device 
        <set>
            id=${
    
    item.id},
            imei=${
    
    item.imei}
        </set>
        where id = ${
    
    item.id}
    </foreach>      
</update>

方法二(不推荐)

查询效率较低,配置多

<update id="updateFenoAppRhHardDeviceList" parameterType="java.util.List">
		<!-- 方法2 -->
		UPDATE rp_feno_app_rh_hard_device 
		<trim prefix="SET" suffixOverrides=",">
         	<trim prefix="MODIFY_DATE = case" suffix="end," >
             	<foreach collection="list" item="i" index="index">
           			WHEN id=#{
    
    i.id,jdbcType=BIGINT} THEN NOW()
          		</foreach>
          	</trim>
         	<trim prefix="IMEI = case" suffix="end," >
             	<foreach collection="list" item="i" index="index">
           			<if test="i.imei!=null and i.imei!=''">
                       	WHEN id=#{
    
    i.id,jdbcType=BIGINT} THEN #{
    
    i.imei,jdbcType=VARCHAR}
                   	</if>
          		</foreach>
          	</trim>
   		</trim>
      	WHERE
   		<foreach collection="list" separator="or" item="i" index="index" >
 			ID = #{
    
    i.id,jdbcType=BIGINT}
     	</foreach>
	</update>

mybatis-plus 使用概述

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官方文档:https://baomidou.com/pages/24112f/

mybatis-plus 批量插入

Service 层 批量插入

// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);

批量插入解析:

类型 参数名 描述
T entity 实体对象
Collection entityList 实体对象集合
int batchSize 插入批次数量

Service 层 批量插入或更新

// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

批量插入或更新解析:

类型 参数名 描述
T entity 实体对象
Collection entityList 实体对象集合
int batchSize 插入批次数量

mybatis-plus 批量更新

// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);

批量更新解析:

类型 参数名 描述
T entity 实体对象
Collection entityList 实体对象集合
int batchSize 插入批次数量

猜你喜欢

转载自blog.csdn.net/qq_44697754/article/details/129439534