mybatis批量插入 SELECT LAST_INSERT_ID() AS ID INSERT INTO rp_feno_app_rh_hard_device
<!-- 批量插入 --><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的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 (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
官方文档:https://baomidou.com/pages/24112f/
// 插入(批量)boolean saveBatch(Collection<T> entityList);// 插入(批量)boolean saveBatch(Collection<T> entityList, int batchSize);
批量插入解析:
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
// 批量修改插入boolean saveOrUpdateBatch(Collection<T> entityList);// 批量修改插入boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
批量插入或更新解析:
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
// 根据ID 批量更新boolean updateBatchById(Collection<T> entityList);// 根据ID 批量更新boolean updateBatchById(Collection<T> entityList, int batchSize);
批量更新解析:
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
来源地址:https://blog.csdn.net/qq_44697754/article/details/129439534
--结束END--
本文标题: mybatis 的批量更新和批量插入(清晰明了)
本文链接: https://www.lsjlt.com/news/402281.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-08
2024-05-08
2024-05-08
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0