springboot entity 插入字段问题.

查看 33|回复 0
作者:Zach369   
数据库中有张表, 表中假设有 id(主键),name(not null default ""), phone(必填)
entity:
public class User implements Serializable {
    private Long id;
    /**
     * 昵称
     */
    private String name;
    /**
     * 手机号
     */
    private String phone;
}
现在有一个 post 接口, 我使用 User 来接收, 然后直接通过 mybatis 插入, 报错
SQLIntegrityConstraintViolationException: Column 'name' cannot be null;
这个错误意思很明显: 因为 post 接口 name 没有传值, 导致 name 就是 String 对象的 null;
我现在设置 private String name = ""; 也能解决, 但是我一个表中可能有很多很多字段 都是这样的,要么 not null default ""/0/1 等, 有什么好的解决办法吗?
框架 springboot 2.7.8 + mybatis
您需要登录后才可以回帖 登录 | 立即注册

返回顶部