Mybatis Mapper代理开发中使用<insert>标签插入数据时,数据库中的数据表主键(id)自增不连续问题的解决方法_mybatis mapper insert

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注网络安全)
img

正文

public void setId(Integer id) {
    this.id = id;
}

public String getBrandName() {
    return brandName;
}

public void setBrandName(String brandName) {
    this.brandName = brandName;
}

public String getCompanyName() {
    return companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}

public Integer getOrdered() {
    return ordered;
}

public void setOrdered(Integer ordered) {
    this.ordered = ordered;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

@Override
public String toString() {
    return "Brand{" +
            "id=" + id +
            ", brandName='" + brandName + '\'' +
            ", companyName='" + companyName + '\'' +
            ", ordered=" + ordered +
            ", description='" + description + '\'' +
            ", status='" + status + '\'' +
            '}';
}

}




---



## 问题描述


#### 这里我们模拟一次数据插入失败,即不提交事务,然后再开启提交事务,即成功提交一次事务


        1、测试类如下:


        



//添加
@Test
public void testAdd() throws IOException {
//存入参数
int status = 1;
String companyName = “frim4”;
String brandName = “某某品牌”;
String description = “天下第一”;
int ordered = 300;

  Brand brand = new Brand();
  brand.setStatus(status);
  brand.setCompanyName(companyName);
  brand.setBrandName(brandName);
  brand.setStatus(status);
  brand.setOrdered(ordered);
  brand.setDescription(description);


  //1.加载mybatis的核心配置文件,获取SqlSessionFactory
  String resource = "mybatis-config.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

  //2.获取执行SqlSession的对象
  SqlSession sqlSession = sqlSessionFactory.openSession();

  //开启自动提交事务

// SqlSession sqlSession = sqlSessionFactory.openSession(true);

  //3.获取mapper对象
  BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

  //4.执行方法
  mapper.auto();
  mapper.add(brand);


  //释放资源
  sqlSession.close();

}


执行改方法后,发现数据表没有任何改变(未提交事务,自动回滚)


![](https://img-blog.csdnimg.cn/0034d3c421f14594b9cde7f127b38644.png)


开启自动提交事务,再次提交数据


        测试类:


        



//添加
@Test
public void testAdd() throws IOException {
//存入参数
int status = 1;
String companyName = “frim4”;
String brandName = “某某品牌”;
String description = “天下第一”;
int ordered = 300;

  Brand brand = new Brand();
  brand.setStatus(status);
  brand.setCompanyName(companyName);
  brand.setBrandName(brandName);
  brand.setStatus(status);
  brand.setOrdered(ordered);
  brand.setDescription(description);


  //1.加载mybatis的核心配置文件,获取SqlSessionFactory
  String resource = "mybatis-config.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

  //2.获取执行SqlSession的对象

// SqlSession sqlSession = sqlSessionFactory.openSession();

  //开启自动提交事务
  SqlSession sqlSession = sqlSessionFactory.openSession(true);

  //3.获取mapper对象
  BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

  //4.执行方法
  mapper.auto();
  mapper.add(brand);



  //释放资源
  sqlSession.close();

}


运行结果:


        ![](https://img-blog.csdnimg.cn/b5cb48aea9a74eb88cf70b8c80aa3eec.png)






---



## 原因分析:



> 
> 唯一键冲突、事务回滚、批量写库操作
> 
> 
> 





---


## 解决方案:



> 
> 1.先删除掉错误的数据(id为5的那一行数据)
> 
> 
> ![](https://img-blog.csdnimg.cn/1481f9af06b547d0b032dc77a816bb47.png)
> 
> 
> 
> 
> 2.增加sql语句:
> 
> 
> 
> ```
>                 alter table tb_brand auto_increment = 1;
> ```
> 
> 新插入的数据会从当前表最大的id开始+1;
> 
> 
> 


在Mybatis中,


        我们在接口中写一个执行上述sql语句的方法:


        BrandMape接口如下:


        



package com.aimin.mapper;

import com.aimin.pojo.Brand;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface BrandMapper {

// 添加
void add(Brand brand);

//解决主键自增不连续
void auto();
}


sql映射文件如下:



<?xml version="1.0" encoding="UTF-8" ?>
<insert id="add" useGeneratedKeys="true" keyProperty="id">

    insert into tb_brand1 (brand_name, company_name, ordered, description, status)
    values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>

<update id="auto">
    alter table tb_brand1 auto_increment = 1;
</update>
```

再次执行测试方法:

//添加
   @Test
   public void testAdd() throws IOException {
      //存入参数
      int id = 1;
      int status = 1;
      String companyName = "frim4";
      String brandName = "某某品牌";
      String description = "天下第一";
      int ordered = 300;

      Brand brand = new Brand();
      brand.setStatus(status);
      brand.setCompanyName(companyName);
      brand.setBrandName(brandName);
      brand.setStatus(status);
      brand.setOrdered(ordered);
      brand.setDescription(description);


      //1.加载mybatis的核心配置文件,获取SqlSessionFactory
      String resource = "mybatis-config.xml";
      InputStream inputStream = Resources.getResourceAsStream(resource);
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

      //2.获取执行SqlSession的对象



## 写在最后

**在结束之际,我想重申的是,学习并非如攀登险峻高峰,而是如滴水穿石般的持久累积。尤其当我们步入工作岗位之后,持之以恒的学习变得愈发不易,如同在茫茫大海中独自划舟,稍有松懈便可能被巨浪吞噬。然而,对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。**


需要完整版PDF学习资源私我





**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)**
![img](https://img-blog.csdnimg.cn/img_convert/be6e4b32ac1c6ef3b27ac4e00526dbf0.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。**


需要完整版PDF学习资源私我





**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注网络安全)**
[外链图片转存中...(img-FMqUpcKt-1713379632250)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值