MyBatis-Plus解决主键自增

MyBatis-Plus主键自增失败

1、先看错误

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c731754]
2022-12-05 21:05:55.322 ERROR 17476 --- [nio-8989-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1
### The error may exist in com/baidu/mapper/SysAccountMapper.java (best guess)
### The error may involve com.baidu.mapper.SysAccountMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO account  ( id, datatime )  VALUES  ( ?, ? )
### Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1
; Data truncation: Out of range value for column 'id' at row 1; nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1] with root cause

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1

大概意思就是说,mybatis-plus添加数据的主键失败。

2、需求

想要数据库中添加数据,成自增的。我明明在数据库设置了主键id自增,但是添加数据的时候没有自增那。

3、解决

1、在数据库中添加确保是自增。
在这里插入图片描述
2、在实体类上页添加自增
在这里插入图片描述

4、自增策略

  • 要想主键自增需要配置如下主键策略
  • 需要在创建数据表的时候设置主键自增
  • 实体字段中配置 @TableId(type = IdType.AUTO)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值