国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

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

在使用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 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 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,參數(shù)為List<Object>

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

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

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內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

相關文章:
主站蜘蛛池模板: 两性色午夜视频免费国产 | 性精品| 夜色综合 | 国内精品91最新在线观看 | 美国一级毛片a | 亚洲精品一区二区观看 | 国产精品久久久久无毒 | 天堂8资源在线官网资源 | 亚洲成a人片在线v观看 | 日本精品一区二区三区视频 | 国内精品久久久久久野外 | 一级二级三级毛片 | 久久www免费人成精品 | 久久国产一区二区三区 | 亚洲欧美综合国产精品一区 | 国产午夜免费不卡精品理论片 | 国产高清厕所盗摄视频 | 国产精品国产三级国产在线观看 | 日韩欧美黄色 | a毛片免费观看 | 欧美激情久久久久久久久 | 亚洲精品二区中文字幕 | 免看一级a毛片一片成人不卡 | 久久生活片| 国产精品国产三级国产专 | a级精品九九九大片免费看 a级毛片免费观看网站 | 亚洲不卡在线观看 | 亚洲欧美日韩综合一区久久 | 成人亚洲国产精品久久 | 精品国产一二三区在线影院 | 国产高清视频免费 | 99精品视频在线免费观看 | 午夜性生活视频 | 久青草青综合在线视频 | 老头老太做爰xxx视频 | 亚洲超大尺度激情啪啪人体 | 99久久精品免费精品国产 | 国产精品视频自拍 | www.亚洲天堂网 | 亚洲专区欧美专区 | 亚洲精品国产一区二区在线 |