MyBatis處理mysql主鍵自動(dòng)增長(zhǎng)出現(xiàn)的不連續(xù)問題解決
問題產(chǎn)生
設(shè)置了mysql主鍵自動(dòng)增長(zhǎng),但因?yàn)閯h除字段的操作導(dǎo)致主鍵不連續(xù)
解決方法
step1:在mapper.xml文件中添加update標(biāo)簽設(shè)置自動(dòng)增長(zhǎng)的增量為1
alter table student AUTO_INCREMENT=1;
<!--StudentMapper.xml文件-->
<mapper namespace="StudentMapper">
...
...
<update id="alter">
alter table student AUTO_INCREMENT=1;
</update>
<insert id="insert" parameterType="com.cooooode.bean.Student" >
insert into student (name,score) values (#{name},#{score});
</insert>
</mapper>step2: 在sqlSession執(zhí)行插入語(yǔ)句前先執(zhí)行更新操作
SqlSession sqlSession = null;
try{
sqlSession = ???
sqlSession.update("StudentMapper.alter"); // 先更新
sqlSession.insert("StudentMapper.insert",student);// 后插入
}catch(...){
//TODO
}finally{
if(sqlSession != null){
sqlSession.commit();
sqlSession.close();
}
}
到此這篇關(guān)于Mybaits處理mysql主鍵自動(dòng)增長(zhǎng)出現(xiàn)的不連續(xù)問題的文章就介紹到這了,更多相關(guān)Mybaits處理mysql主鍵自動(dòng)增長(zhǎng)出現(xiàn)的不連續(xù)問題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+MyBatisPlus對(duì)Map中Date格式轉(zhuǎn)換處理的方法詳解
在?SpringBoot?項(xiàng)目中,?如何統(tǒng)一?JSON?格式化中的日期格式。本文將為大家介紹一種方法:利用MyBatisPlus實(shí)現(xiàn)對(duì)Map中Date格式轉(zhuǎn)換處理,需要的可以參考一下2022-10-10
SpringBoot應(yīng)用部署于外置Tomcat容器的方法
Spring Boot結(jié)合ECharts案例演示示例
IntelliJ IDEA中如何構(gòu)建Spring Boot的項(xiàng)目
SpringBoot多文件分布式上傳功能實(shí)現(xiàn)
Mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)權(quán)限的示例代碼
基于SpringBoot實(shí)現(xiàn)輕量級(jí)的動(dòng)態(tài)定時(shí)任務(wù)調(diào)度的方法

