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屬性值
相關文章:
1. nignx - docker內(nèi)nginx 80端口被占用2. docker網(wǎng)絡端口映射,沒有方便點的操作方法么?3. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?4. docker綁定了nginx端口 外部訪問不到5. docker不顯示端口映射呢?6. angular.js - angular內(nèi)容過長展開收起效果7. java - 為什么此私有靜態(tài)變量能被訪問呢?8. fragment - android webView 返回后怎么禁止重新渲染?9. php - mysql 模糊搜索問題10. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?
