MyBatis处理mysql主键自动增长出现的不连续问题解决
更新时间:2022年01月24日 15:34:23 作者:__师寇__
本文主要介绍了MyBatis处理mysql主键自动增长出现的不连续问题解决,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
问题产生
设置了mysql主键自动增长,但因为删除字段的操作导致主键不连续
解决方法
step1:在mapper.xml文件中添加update标签设置自动增长的增量为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执行插入语句前先执行更新操作
SqlSession sqlSession = null;
try{
sqlSession = ???
sqlSession.update("StudentMapper.alter"); // 先更新
sqlSession.insert("StudentMapper.insert",student);// 后插入
}catch(...){
//TODO
}finally{
if(sqlSession != null){
sqlSession.commit();
sqlSession.close();
}
}
到此这篇关于Mybaits处理mysql主键自动增长出现的不连续问题的文章就介绍到这了,更多相关Mybaits处理mysql主键自动增长出现的不连续问题内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
SpringBoot+MyBatisPlus对Map中Date格式转换处理的方法详解
在 SpringBoot 项目中, 如何统一 JSON 格式化中的日期格式。本文将为大家介绍一种方法:利用MyBatisPlus实现对Map中Date格式转换处理,需要的可以参考一下2022-10-10
IntelliJ IDEA中如何构建Spring Boot的项目
这篇文章主要介绍了IntelliJ IDEA中如何构建Spring Boot的项目问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-07-07


最新评论