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、在数据库中添加确保是自增。
MyBatis-Plus解决主键自增_第1张图片
2、在实体类上页添加自增
MyBatis-Plus解决主键自增_第2张图片

4、自增策略

  • 要想主键自增需要配置如下主键策略
  • 需要在创建数据表的时候设置主键自增
  • 实体字段中配置 @TableId(type = IdType.AUTO)

你可能感兴趣的:(MyBatis-Plus,mybatis,mysql,java)