mybatis如何在一個(gè)update標(biāo)簽中寫多條update語句
mybatis如何在一個(gè)update標(biāo)簽中寫多條update語句
在mapper里,一個(gè)update標(biāo)簽中寫了多條update語句,在執(zhí)行時(shí)會拋出SQL異常,是因?yàn)樵趍ybatis中默認(rèn)不支持同時(shí)執(zhí)行多條語句。
語句如下:
<update id="updateUserState"> update sys_user set sys_user_state = #{param2} where sys_user_id = #{param1}; update sys_user set sys_user_state = #{param2} where sys_user_id = #{param1}; update sys_user set sys_user_state = #{param2} where sys_user_id = #{param1}; </update>
解決方案
在propertes 或者yml配置文件中找到mysql的jdbc鏈接,在其后追加&allowMultiQueries=true
例如:
url: jdbc:mysql://127.0.0.1:3306/${db.name}?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&allowMultiQueries=true
mybatis支持批量update
有的時(shí)候我們需要將我們拼接的多條update語句放在mysql一個(gè)<update>標(biāo)簽去執(zhí)行,像平常那樣是不行的。
這就需要我們改一些東西了,首先我們需要在我們jdbcurl上拼接上allowMultiQueries=true,如下:
url="jdbc:mysql://localhost:8066/TESTDB?allowMultiQueries=true"
這個(gè)時(shí)候我們就可以在我們的<update>標(biāo)簽中寫多個(gè)update語句了
如果update語句太多的話,比如有個(gè)上千條:我們可以在mysql 的my.cnf中配置如下:
wait_timeout=31536000 interactive_timeout=31536000
oracle數(shù)據(jù)庫
<update id="updatebatch" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";"> update table_name <set > <if test="item.status != null" > status = #{item.status,jdbcType=INTEGER}, </if> </set> where id = #{item.id,jdbcType=BIGINT} </foreach> </update>
mysql數(shù)據(jù)庫
mysql數(shù)據(jù)庫采用一下寫法即可執(zhí)行,但是數(shù)據(jù)庫連接必須配置:&allowMultiQueries=true
例如:
jdbc:mysql://192.168.1.232:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
<update id="updatebatch" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update table_name <set > <if test="item.status != null" > status = #{item.status,jdbcType=INTEGER}, </if> </set> where id = #{item.id,jdbcType=BIGINT} </foreach> </update>
當(dāng)然我們頁可以在代碼中動(dòng)態(tài)拼接如下:
public int batchUpdateTfrData(IRequest r, List<Long> aeTfrIdList, String mes, String accountStatus) { StringBuffer str = new StringBuffer(); for(int i = 0; i < aeTfrIdList.size(); ++i) { str.append("update hsae_ae_tfr_events set ACCOUNTING_STATUS "); str.append(" = '" + accountStatus + "'"); if (mes != null) { str.append(", ACCOUNTING_REMARKS"); str.append(" = '" + mes + "'"); } str.append(CommonUtils.whoUpdate(r)); str.append(" where TFR_EVENT_ID ="); str.append(aeTfrIdList.get(i)); str.append(";"); } this.aeEventBatchesMapper.updateSourceData(str.toString()); return aeTfrIdList.size(); }
xml:
<update id="updateSourceData" parameterType="string"> ${sqlText} </update>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案
這篇文章主要介紹了Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07MyBatis動(dòng)態(tài)創(chuàng)建表的實(shí)例代碼
在項(xiàng)目需求中,我們經(jīng)常會遇到動(dòng)態(tài)操作數(shù)據(jù)表的需求,常見的我們會把日志、設(shè)備實(shí)時(shí)位置信息等存入數(shù)據(jù)表,并且以一定時(shí)間段生成一個(gè)表來存儲。接下來通過本文給大家介紹MyBatis動(dòng)態(tài)創(chuàng)建表的方法,感興趣的朋友一起看看吧2018-07-07springboot配置文件綁定實(shí)現(xiàn)解析
這篇文章主要介紹了springboot配置文件綁定實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01