mybatis 为何我的id已经设置了主键自增,仍然报id不能为空?

//上图是表信息

//控制台错误信息

### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'id' cannot be null
### The error may exist in com/dun/mapper/AccountMapper.java (best guess)
### The error may involve com.dun.mapper.AccountMapper.save-Inline
### The error occurred while setting parameters
### SQL: insert into accounts values(?,?,?)
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'id' cannot be null
; Column 'id' cannot be null; nested exception is java.sql.SQLIntegrityConstraintViolationException: Column 'id' cannot be null

//代码信息

public interface AccountMapper {

    @Insert("insert into accounts values(#{id},#{name},#{money})")
    public void save(Account account);

    @Select("select * from accounts")
    public List<Account> findAll();
}

看不住有自增 - -

既然是自增,你就不要传id这个字段了。你传入的id是null,肯定报错啊。只保存其他字段就行了

 

1.首先你的数据库必须 整型 必须选上
2.mybatis在传参的时间就不要为id默认赋值了;
 

也可以参考:https://blog.csdn.net/qq_16498553/article/details/108229214

有问题也可以私聊。

mybatis 后面给你将sql解析成了 insert into tableA(id,field1field2)(null,value1,value2);

看见了你的insert sql语句  语句中你没有写字段名称 mysql的解析器会优化你的sql 加上字段名  sql不写字段名是坏毛病