Java中用Mybatis插入mysql報(bào)主鍵重復(fù)的解決方案
Mybatis插入mysql報(bào)主鍵重復(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集成EasyExcel的應(yīng)用場景分析
這篇文章主要介紹了SpringBoot集成EasyExcel的應(yīng)用場景,java領(lǐng)域解析、生成excel比較有名的框架有apache poi、jxl等,今天通過實(shí)例代碼給大家詳細(xì)介紹,需要的朋友可以參考下2021-07-07
springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁面
這篇文章主要介紹了springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
SpringBoot實(shí)現(xiàn)Excel讀取的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)Excel讀取的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

