java - mybatis mysql 如何實現(xiàn)upsert功能?
問題描述
1、數(shù)據(jù)不存在insert;2、數(shù)據(jù)存在update;3、表中有一個唯一約束;并根據(jù)該約束執(zhí)行具體的插入或者修改操作。
————————————————————————————
insert into T_name (uid, app_id,createTime,modifyTime) values(111, 1000000,’2017-03-07 10:19:12’,’2017-03-07 10:19:12’) on duplicate key update uid=111, app_id=1000000, createTime=’2017-03-07 10:19:12’,modifyTime=’2017-05-07 10:19:12’
如何把上邊的sql,用mybatis改寫?
問題解答
回答1:<insert parameterType='Model'> insert into T_name(uid, name, age, balance) values (100,'yangyang', 23, 100000000) on duplicate key update balance=balance + 100</insert>
以上代碼,自己測試可以的。
回答2:<insert parameterType='Model'>
insert into T_name (uid, name, age, balance) values (#{uid,jdbcType=VARCHAR},#{name}, 23, 100000000)on duplicate key update balance=balance + 100
</insert>
my mybatis 中 使用#{} 獲取vo屬性值
相關(guān)文章:
1. android-studio - Android Studio 運行項目的時候一堆警告,跑步起來!?2. dockerfile - [docker build image失敗- npm install]3. mysql - 新浪微博中的關(guān)注功能是如何設(shè)計表結(jié)構(gòu)的?4. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。5. 如何解決Centos下Docker服務(wù)啟動無響應(yīng),且輸入docker命令無響應(yīng)?6. angular.js - 關(guān)于$apply()7. MySQL數(shù)據(jù)庫中文亂碼的原因8. 表單提交驗證,沒反應(yīng),求老師指點9. nignx - docker內(nèi)nginx 80端口被占用10. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???
