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

import java.util.Map;

public interface BrandMapper {

// 添加
void add(Brand brand);

}


        2.配置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>
```
测试方法:
//添加
   @Test
   public void testAdd() throws IOException {
      //存入参数
 
      int status = 1;
      String companyName = "firm4";
      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();

      //5.传入band类
      mapper.add(brand);



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

Brand类:

public class Brand {
  
     private Integer id;
     private String brandName ;
     private String 
  • 22
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值