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

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

mybatis update更新字段的使用操作

瀏覽:2日期:2023-10-20 11:05:15

多個mapper方法,更新單字段

說實話不太推薦,因為如果有10個字段要更新,難道寫10個方法。

但是實際中很多人都這么寫。

通用mapper方法,java代碼控制字段

特點是一個mapper方法包含所有字段,不為空的就update。

但是需要控制入?yún)ⅲ话阌?中方式:

new 一個對象然后set id和要改的字段

如果字段多比較費勁,需要一個一個set。

查詢出對象,然后set要改的字段

這2種方式差不多,就是代碼看起來不一樣。

特別注意,定位字段不要加if

要更新的字段加if沒有什么問題

但是定位條件不要加if,因為萬一忘記傳遞了,變成沒有where條件,那么條數(shù)不可控了。搞不好把全表更新了,可就萬劫不復了。

補充:mybatis執(zhí)行批量更新update

目前想批量更新,如果update的值是相同的話,很簡單,

update table set column=’...’ where id in (1,2,3)l

這樣的sql就可以了。Mybatis中這樣寫就行

<update parameterType='java.util.Map' > UPDATE STUDENT SET name = #{name} WHERE id IN <foreach collection='idList' index='index' item='item' open='(' separator=',' close=')'> #{item} </foreach></update>

但是這樣的需求很少,一般是有個集合,每個元素中的值是不一樣的,然后需要一次性更新。一般的處理方式是使用for循環(huán)。這樣的效率較低,當數(shù)據(jù)量大時,期望有種一次性插入的操作。如果使用的是mysql,有

insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo) on duplicate key update

replace into table (aa,bb,cc) values(xxx,xxx,xxx),(ooo,ooo,ooo),(ccc,ccc,ccc)

兩種方式可以處理。

當前數(shù)據(jù)庫是oracle,可以使用case when來拼成一長串sql處理

UPDATE mytable SET myfield = CASE id WHEN 1 THEN ’value’ WHEN 2 THEN ’value’ WHEN 3 THEN ’value’ ENDWHERE id IN (1,2,3)

實際上這種方式對于mysql也有效。

最開始的時候,想著寫一系列并列的更新語句就可以了

<update parameterType='java.util.List'><foreach collection='list' item='item' index='index' separator=';' open='' close=''> update REGION_CODE set CODE=#{item.Code,jdbcType=VARCHAR}, NAME=#{item.Name,jdbcType=VARCHAR} where ID = #{item.id,jdbcType=DECIMAL}</foreach></update>

這樣直接報錯,因為Mybatis映射文件中的sql語句不允許 ; 符號。

兩種方法:

第一種:需要在db鏈接url后面帶一個參數(shù) &allowMultiQueries=true

即:

jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true

第二種:按照可行的case when處理方式,Mybatis映射文件書寫方式如下:

<update parameterType='java.util.List'> update REGION_CODE set CODE= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR} </foreach> ,NAME= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR} </foreach> where ID in <foreach collection='list' index='index' item='item' separator=',' open='(' close=')'> #{item.id,jdbcType=DECIMAL} </foreach></update>

至此,批量更新功能完成。

項目中實際使用案例:

<update parameterType='java.util.List'> update user_credit_black_list set type= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=BIGINT} then #{item.type,jdbcType=VARCHAR} </foreach> ,user_id= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=BIGINT} then #{item.userId,jdbcType=BIGINT} </foreach> ,update_time= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=BIGINT} then #{item.updateTime,jdbcType=TIMESTAMP} </foreach> ,delete_flg= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=BIGINT} then #{item.deleteFlg,jdbcType=BIT} </foreach> ,update_code= <foreach collection='list' item='item' index='index' separator=' ' open='case ID' close='end'> when #{item.id,jdbcType=BIGINT} then #{item.updateCode,jdbcType=BIGINT} </foreach> where ID in <foreach collection='list' index='index' item='item' separator=',' open='(' close=')'> #{item.id,jdbcType=BIGINT} </foreach> </update>

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

相關文章:
主站蜘蛛池模板: 最新步兵社区在线观看 | 久久黄色精品视频 | 成年女人在线观看片免费视频 | 经典香港a毛片免费观看 | 亚洲免费视频播放 | 国产性自爱拍偷在在线播放 | 小明日韩在线看看永久区域 | 国产成人综合欧美精品久久 | 成年女人毛片免费播放视频m | 久久99爱视频 | 毛片亚洲毛片亚洲毛片 | 97国产在线视频公开免费 | 久久99九九精品免费 | 亚洲国产专区 | 国产精品视频免费观看调教网 | 深夜福利视频网站 | 国产在线精品一区二区 | 精品久久久久中文字幕日本 | 中国日本高清免费视频网 | 久久精品亚洲综合一品 | 欧美色视频日本片高清在线观看 | 午夜手机视频 | 亚洲第一成人在线 | 亚洲欧美另类日本久久影院 | 欧美黑寡妇特a级做爰 | 黄色三级网站免费 | japonensis国产福利| 欧美一级性 | 伊人短视频 | 亚洲成人一级片 | 精品国产香蕉伊思人在线 | 亚洲三级中文字幕 | 普通话对白国产精品一级毛片 | avav男人天堂 | 中文字幕一区二区三区亚洲精品 | 久久精品视频7 | 国产在线一区二区三区欧美 | 国产成人精品男人的天堂网站 | 好吊妞998视频免费观看在线 | 国内国外精品一区二区 | 第一色网站 |