Java中用Mybatis插入mysql報主鍵重復(fù)的解決方案
Mybatis插入mysql報主鍵重復(fù)的問題
首先思路是這樣的,先去數(shù)據(jù)表里面去找有沒有這個主鍵的數(shù)據(jù)(如果有會有返回值,如果沒有則返回null),如果有則對該條數(shù)據(jù)進(jìn)行更新操作,如果沒有,則對數(shù)據(jù)表進(jìn)行插入操作。
原來數(shù)據(jù)表中有這些數(shù)據(jù)。
數(shù)據(jù)表對應(yīng)的bean的結(jié)構(gòu)如下:
public class DataBean { String key; String value; public DataBean() { } public DataBean(String key, String value) { this.key = key; this.value = value; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "DataBean{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}'; } }
下面是我Mapper內(nèi)的內(nèi)容:
<insert id="InsertDataToTestTable" parameterType="test.bean.DataBean"> insert into testtable values(#{key},#{value}) </insert> <update id="UpdateDataToTestTable" parameterType="test.bean.DataBean"> UPDATE testtable SET value=#{value} WHERE `key`=#{key} </update> <select id="SelectDataToTestTable" parameterType="int" resultType="test.bean.DataBean"> SELECT * from testtable where `key`=#{key} </select>
首先通過SqlSession.selectOne去查,看我此次想要插入的bean是否存在于表里面(表的主鍵為key),如果存在,那么select語句會返回Databean對象,此時就可以去對表中數(shù)據(jù)進(jìn)行相應(yīng)的value更新操作了。
如果不存在的話,那么select語句返回的是null,此時就可以進(jìn)行相應(yīng)的插入操作,將數(shù)據(jù)插入到表中。
下面是測試代碼:
public static void main(String[] args) { SqlSession session = SqlSessionFactoryUtil.getSqlSession(); DataBean dataBean=new DataBean(); dataBean.setKey("123"); dataBean.setValue("1111"); if(session.selectOne("DataMapper.SelectDataToTestTable",Integer.valueOf(dataBean.getKey()))!=null){ //查看select語句輸出結(jié)果 System.out.println(session.selectOne("DataMapper.SelectDataToTestTable",Integer.valueOf(dataBean.getKey()))); session.update("DataMapper.UpdateDataToTestTable",dataBean); }else { session.insert("DataMapper.InsertDataToTestTable", dataBean); } session.commit(); }
現(xiàn)在我的表里面是有key=123,value=111的記錄,那么我這次執(zhí)行程序會將其value更新為1111。下面請看輸出結(jié)果以及表中數(shù)據(jù)更改。
select操作在查詢key為123的時候返回的值。
并且數(shù)據(jù)庫的記錄已經(jīng)做了對應(yīng)的更改。
下面我進(jìn)行插入記錄key:123333 value:123123,我們知道表中是沒有key為123333的記錄的,所以select操作會返回null,然后執(zhí)行insert操作而不是update操作。
public static void main(String[] args) { SqlSession session = SqlSessionFactoryUtil.getSqlSession(); DataBean dataBean=new DataBean(); dataBean.setKey("123333"); dataBean.setValue("123123"); if(session.selectOne("DataMapper.SelectDataToTestTable",Integer.valueOf(dataBean.getKey()))!=null){ //查看select語句輸出結(jié)果 System.out.println(session.selectOne("DataMapper.SelectDataToTestTable",Integer.valueOf(dataBean.getKey()))); session.update("DataMapper.UpdateDataToTestTable",dataBean); }else { session.insert("DataMapper.InsertDataToTestTable", dataBean); } session.commit(); }
至此,當(dāng)數(shù)據(jù)庫插入數(shù)據(jù)的時候遇到主鍵重復(fù)的錯誤問題已經(jīng)解決,我這里只是提供一種思路和一些簡單的實(shí)現(xiàn),希望能對你們有幫助~
Mybatis返回插入的主鍵
<insert id="insertTask" parameterType="Task" useGeneratedKeys="true" keyProperty="id">
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解springboot項(xiàng)目啟動時如何排除用不到的bean
使用springboot開發(fā)項(xiàng)目,我們有時候會排除一些項(xiàng)目里面用不到的bean,不然的話項(xiàng)目啟動會報錯,這種情況通常是發(fā)生在什么場景里呢,以及如何解決呢,今天咱們就聊一聊2024-01-01Mybatis-plus實(shí)現(xiàn)主鍵自增和自動注入時間的示例代碼
這篇文章主要介紹了Mybatis-plus實(shí)現(xiàn)主鍵自增和自動注入時間的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java的動態(tài)綁定與雙分派_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java的動態(tài)綁定與雙分派,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.ide
這篇文章主要介紹了解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.idea.maven - Cannot reconnect.問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08基于Docker的K8s(Kubernetes)集群部署方案
這篇文章主要介紹了基于Docker的K8s(Kubernetes)集群部署方案,文中介紹了安裝k8s的可視化界面的相關(guān)操作,需要的朋友可以參考下2024-01-01JavaCV與FFmpeg音視頻流處理技巧總結(jié)大全
JavaCV是一個開源的Java接口,它為幾個著名的計(jì)算機(jī)視覺庫(如OpenCV、FFmpeg)提供了Java封裝,這篇文章主要給大家介紹了關(guān)于JavaCV與FFmpeg音視頻流處理技巧總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-05-05Redisson RedLock紅鎖加鎖實(shí)現(xiàn)過程及原理
本文主要介紹了Redis中Redisson紅鎖(Redlock)使用原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02