午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Mybatis批量操作sql寫法示例(批量新增、更新)

瀏覽:30日期:2023-10-19 11:10:34

在使用foreach時,collection屬性值的三種情況:

如果傳入的參數類型為List時,collection的默認屬性值為list,同樣可以使用@Param注解自定義keyName;

如果傳入的參數類型為array時,collection的默認屬性值為array,同樣可以使用@Param注解自定義keyName;

如果傳入的參數類型為Map時,collection的屬性值可為三種情況:

1.遍歷map.keys;2.遍歷map.values;3.遍歷map.entrySet()

批量Insert,參數為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,參數為Map<Long, List<Long>>

void batchSaveGroupAndUser(@Param('map') Map<Long, List<Long>> groupUserMap);

接下來看sql如何寫:

<insert 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,參數為Map<String, String>

void batchInsert(@Param('map') Map<String, String> map);

<insert 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 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 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,參數為List<Object>

**注意:**在執行批量Update的時候,數據庫的url配置需要添加一項參數:&allowMultiQueries=true

如果沒有這個配置參數的話,執行下面的更新語句會報錯:

Mybatis批量操作sql寫法示例(批量新增、更新)

正確的sql寫法如下:

<update 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>

總結

到此這篇關于Mybatis批量操作sql寫法的文章就介紹到這了,更多相關Mybatis批量操作sql內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 日本天堂在线 | 国产区在线看 | 国产精品自产拍 | 草久久免费视频 | www.狠狠撸.com | av在线免费观看网站 | 久久国产精品二区 | 久久1024| 精品欧美激情精品一区 | 午夜亚洲天堂 | 国产精品每日更新 | 91视频综合| 国产精品国产三级国产aⅴ中文 | 91嫩草丨国产丨精品 | 四虎影视在线免费观看 | 草久久免费视频 | 国产精品嫩草影院俄罗斯 | 午夜免费视频 | 日韩一区二区三区在线观看 | 天堂资源站 | 一卡二卡在线 | 成人久久久久久 | 6080成人 | 中文字幕四区 | 国产一二区在线观看 | av成人在线看 | 午夜精品一区二区三区视频 | 五月婷婷六月天 | 午夜影片| 久久久久久久久久久网站 | 麻豆久久久 | 蜜桃av噜噜一区二区三区麻豆 | 一区二区三区不卡视频 | 国产91精品一区二区 | 欧美日韩视频免费观看 | 亚洲一区图片 | 亚洲美女毛片 | 亚洲黄色一区二区三区 | 精品国产自 | 成人免费视频一区 | 精品日韩一区二区三区 |