Java中用Mybatis插入mysql報(bào)主鍵重復(fù)的解決方案
Mybatis插入mysql報(bào)主鍵重復(fù)的問(wèn)題
首先思路是這樣的,先去數(shù)據(jù)表里面去找有沒(méi)有這個(gè)主鍵的數(shù)據(jù)(如果有會(huì)有返回值,如果沒(méi)有則返回null),如果有則對(duì)該條數(shù)據(jù)進(jìn)行更新操作,如果沒(méi)有,則對(duì)數(shù)據(jù)表進(jìn)行插入操作。
原來(lái)數(shù)據(jù)表中有這些數(shù)據(jù)。

數(shù)據(jù)表對(duì)應(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>首先通過(guò)SqlSession.selectOne去查,看我此次想要插入的bean是否存在于表里面(表的主鍵為key),如果存在,那么select語(yǔ)句會(huì)返回Databean對(duì)象,此時(shí)就可以去對(duì)表中數(shù)據(jù)進(jìn)行相應(yīng)的value更新操作了。
如果不存在的話,那么select語(yǔ)句返回的是null,此時(shí)就可以進(jìn)行相應(yīng)的插入操作,將數(shù)據(jù)插入到表中。
下面是測(cè)試代碼:
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語(yǔ)句輸出結(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í)行程序會(huì)將其value更新為1111。下面請(qǐng)看輸出結(jié)果以及表中數(shù)據(jù)更改。

select操作在查詢key為123的時(shí)候返回的值。

并且數(shù)據(jù)庫(kù)的記錄已經(jīng)做了對(duì)應(yīng)的更改。
下面我進(jìn)行插入記錄key:123333 value:123123,我們知道表中是沒(méi)有key為123333的記錄的,所以select操作會(huì)返回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語(yǔ)句輸出結(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ù)庫(kù)插入數(shù)據(jù)的時(shí)候遇到主鍵重復(fù)的錯(cuò)誤問(wèn)題已經(jīng)解決,我這里只是提供一種思路和一些簡(jiǎn)單的實(shí)現(xiàn),希望能對(duì)你們有幫助~
Mybatis返回插入的主鍵
<insert id="insertTask" parameterType="Task" useGeneratedKeys="true" keyProperty="id">
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成EasyExcel的應(yīng)用場(chǎng)景分析
這篇文章主要介紹了SpringBoot集成EasyExcel的應(yīng)用場(chǎng)景,java領(lǐng)域解析、生成excel比較有名的框架有apache poi、jxl等,今天通過(guò)實(shí)例代碼給大家詳細(xì)介紹,需要的朋友可以參考下2021-07-07
springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁(yè)面
這篇文章主要介紹了springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
SpringBoot實(shí)現(xiàn)Excel讀取的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)Excel讀取的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

