java - mybatis mysql 如何實(shí)現(xiàn)upsert功能?
問(wèn)題描述
1、數(shù)據(jù)不存在insert;2、數(shù)據(jù)存在update;3、表中有一個(gè)唯一約束;并根據(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改寫?
問(wèn)題解答
回答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>
以上代碼,自己測(cè)試可以的。
回答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. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker-compose中volumes的問(wèn)題3. boot2docker無(wú)法啟動(dòng)4. nignx - docker內(nèi)nginx 80端口被占用5. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.6. docker容器呢SSH為什么連不通呢?7. node.js - antdesign怎么集合react-redux對(duì)input控件進(jìn)行初始化賦值8. dockerfile - 為什么docker容器啟動(dòng)不了?9. java - SSH框架中寫分頁(yè)時(shí)service層中不能注入分頁(yè)類10. 關(guān)于docker下的nginx壓力測(cè)試

網(wǎng)公網(wǎng)安備