广告
返回顶部
首页 > 资讯 > 数据库 >mybatis 的批量更新和批量插入(清晰明了)
  • 730
分享到

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

mybatisjavamysql 2023-09-10 12:09:24 730人浏览 八月长安
摘要

mybatis批量插入 SELECT LAST_INSERT_ID() AS ID INSERT INTO rp_feno_app_rh_hard_device

在这里插入图片描述

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);

批量插入解析:

类型参数名描述
Tentity实体对象
CollectionentityList实体对象集合
intbatchSize插入批次数量

Service 层 批量插入或更新

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

批量插入或更新解析:

类型参数名描述
Tentity实体对象
CollectionentityList实体对象集合
intbatchSize插入批次数量

mybatis-plus 批量更新

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

批量更新解析:

类型参数名描述
Tentity实体对象
CollectionentityList实体对象集合
intbatchSize插入批次数量

来源地址:https://blog.csdn.net/qq_44697754/article/details/129439534

您可能感兴趣的文档:

--结束END--

本文标题: mybatis 的批量更新和批量插入(清晰明了)

本文链接: https://www.lsjlt.com/news/402281.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作