Mybatis批量操作sql寫法示例(批量新增、更新)
在使用foreach時,collection屬性值的三種情況:
如果傳入的參數(shù)類型為List時,collection的默認屬性值為list,同樣可以使用@Param注解自定義keyName;
如果傳入的參數(shù)類型為array時,collection的默認屬性值為array,同樣可以使用@Param注解自定義keyName;
如果傳入的參數(shù)類型為Map時,collection的屬性值可為三種情況:
1.遍歷map.keys;
2.遍歷map.values;
3.遍歷map.entrySet()
批量Insert,參數(shù)為List<Object>
mysql的批量新增sql的寫法示例,先看一下mapper的寫法;
void batchSaveUser(List<SysUser> userList);
接下來看sql如何寫:
<insert id="batchSaveUser"> insert into sys_user (ding_user_id, username, nickname, password, email, mobile, avatar, creator_id, create_time, updator_id, update_time, is_delete) values <foreach collection="list" item="user" separator=","> ( #{user.dingUserId}, #{user.username}, #{user.nickname}, #{user.password}, #{user.email}, #{user.mobile}, #{user.avatar}, #{user.creatorId}, now(), #{user.updatorId}, now(), 0 ) </foreach> </insert>
批量Insert,參數(shù)為Map<Long, List<Long>>
void batchSaveGroupAndUser(@Param("map") Map<Long, List<Long>> groupUserMap);
接下來看sql如何寫:
<insert id="batchSaveGroupAndUser" parameterType="java.util.Map"> insert into sys_group_member (group_id, user_id, creator_id, create_time) values <foreach collection="map.keys" item="groupId" separator=","> <foreach collection="map[groupId]" item="userId" separator=","> ( #{groupId}, #{userId}, 'admin', now() ) </foreach> </foreach> </insert>
批量Insert,參數(shù)為Map<String, String>
void batchInsert(@Param("map") Map<String, String> map);
<insert id="batchInsert" parameterType="java.util.Map"> insert into brand_info (code, `name`, is_delete, create_time) values <foreach collection="map.entrySet()" index="key" item="value" open="(" close=")" separator=","> #{key}, #{value}, 0, now() </foreach> </insert>
如果是只需要遍歷key,寫法則是collection=“map.keys”
<insert id="batchSave" parameterType="java.util.Map"> insert into brand_info (code, is_delete, create_time) values <foreach collection="map.keys" item="key" open="(" close=")" separator=","> #{key}, 0, now() </foreach> </insert>
同理,如果是只需要遍歷value,寫法則是collection=“map.values”
<insert id="batchSave" parameterType="java.util.Map"> insert into brand_info (code, is_delete, create_time) values <foreach collection="map.values" item="value" open="(" close=")" separator=","> #{value}, 0, now() </foreach> </insert>
批量Update,參數(shù)為List<Object>
**注意:**在執(zhí)行批量Update的時候,數(shù)據(jù)庫的url配置需要添加一項參數(shù):&allowMultiQueries=true
如果沒有這個配置參數(shù)的話,執(zhí)行下面的更新語句會報錯:
正確的sql寫法如下:
<update id="batchUpdateCorporation" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" separator=";"> update sys_corporation set <if test="item.name != null and item.name !=''"> `name` = #{item.name}, </if> <if test="item.code != null and item.code !=''"> code = #{item.code}, </if> <if test="item.parentCode != null and item.parentCode !=''"> parent_code = #{item.parentCode}, </if> updater = 'system', update_time = now() where id = #{item.id} </foreach> </update>
總結(jié)
到此這篇關(guān)于Mybatis批量操作sql寫法的文章就介紹到這了,更多相關(guān)Mybatis批量操作sql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea 普通文件夾 轉(zhuǎn)換成 module操作
這篇文章主要介紹了idea 普通文件夾 轉(zhuǎn)換成 module操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08springboot jpaRepository為何一定要對Entity序列化
這篇文章主要介紹了springboot jpaRepository為何一定要對Entity序列化,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12springboot使用RedisRepository操作數(shù)據(jù)的實現(xiàn)
本文主要介紹了springboot使用RedisRepository操作數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-05-05SpringCloud-Hystrix-Dashboard客戶端服務(wù)監(jiān)控的實現(xiàn)方法
這篇文章主要介紹了SpringCloud-Hystrix-Dashboard客戶端服務(wù)監(jiān)控的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法解析
這篇文章主要介紹了SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SSH框架網(wǎng)上商城項目第16戰(zhàn)之Hibernate二級緩存處理首頁熱門顯示
這篇文章主要介紹了SSH框架網(wǎng)上商城項目第16戰(zhàn)之Hibernate的二級緩存處理首頁的熱門顯示,感興趣的小伙伴們可以參考一下2016-06-06關(guān)于Java應(yīng)用日志與Jaeger的trace關(guān)聯(lián)的問題
這篇文章主要介紹了Java應(yīng)用日志如何與Jaeger的trace關(guān)聯(lián),通過jaeger發(fā)現(xiàn)這十次請求中有一次耗時特別長,想定位一下具體原因,感興趣的朋友跟隨小編一起看看吧2022-01-01