springboot entity 插入字段问题.

2023-02-14 23:30:47 +08:00
 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

1664 次点击
所在节点    Java
18 条回复
atonganan
2023-02-15 07:27:00 +08:00
public class User implements Serializable {
private Long id;
/**
* 昵称
*/
private String name = "";
/**
* 手机号
*/
private String phone;
}
DoctorDeng
2023-02-15 08:54:00 +08:00
api 参数不合法不应该检验一下么
cslive
2023-02-15 08:54:55 +08:00
按道理来说不应该啊,不都是判断是否为空再插入的吗
freedom0
2023-02-15 09:18:12 +08:00
@NotBlank(message = "name is not null")
private String name;
lybcyd
2023-02-15 09:51:55 +08:00
这个要看你的业务逻辑,如果名字本身就是必填项,那就加一个校验,如果没填名字就强制用户填写。如果是可选,那就默认为空字符串。
Zach369
2023-02-15 10:02:31 +08:00
@atonganan 我上面写了, 可以这么弄,直接="", 但是我一个 class 有太多这样的了, 我不想每个都写.


@DoctorDeng @cslive name 本身就是可选参数, 我数据库设置为 not null default "", api 怎么验证那?如果别人不传, @RequestBody 接收, name 就是 Null

@lybcyd
wangxin3
2023-02-15 10:35:45 +08:00
可选字段数据库为什么要 not null 呢
xiaohundun
2023-02-15 11:40:29 +08:00
你自己实现一个 MessageConverters ,然后 mvc 里面配置好 MessageConverters
timethinker
2023-02-15 13:15:13 +08:00
如果你的数据库字段设置为 NOT NULL ,默认值仅在你未指定字段值的时候才有用,举个例子,如下的 SQL 可以正常工作,因为没有指定 name ,因此默认值生效了:
INSERT INTO user (phone) VALUES ('XXX')

但是假如 SQL 为下面这种,因为 name 字段不允许为空,因此这个插入是不合法的:
INSERT INTO user(name, phone) VALUES(NULL, 'XXX')

Mybatis 可以在 XML 中使用 if 来判断某个值是否为空,然后再决定最后构造的 SQL ,比如这种:
<insert id="insertUser" keyColumn="id" >
INSERT INTO user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="phone != null">
phone,
</if>
</trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="isDeleted != null">
#{name},
</if>
<if test="paymentId != null">
#{phone},
</if>
</trim>
</insert>
timethinker
2023-02-15 13:18:54 +08:00
例子中自行把最后的 isDeleted 和 paymentId 改为 name 和 phone ,我这里复制过来的,这点忘了更改。
OldCarMan
2023-02-15 13:59:06 +08:00
@wangxin3 不给默认值,有时会出现一些使用上的问题,比如:某些函数像 sum,count 之类的有时失效,group by 聚合数据时有时也有问题,另外某些场景索引会失效。所以一般情况下都会给个默认值。当然以上纯属个人看法。
vagusss
2023-02-15 14:12:47 +08:00
我看你这情况,数据库字段值只设置默认值就好了, 不要设置 not null, 这样插入时如果 name 为 null ,就会填充默认值
vagusss
2023-02-15 14:16:20 +08:00
我好像说错了, 当我没说.........
Kontinue
2023-02-15 14:22:18 +08:00
你这前后矛盾,本来就是可 null 的,数据库还设置 not null 。。。
JPA 里有个 @DynamicInsert 的注解,如果字段为 null ,就不拼接到 sql ,不知道 mybatis 里有没有类似的
c6h6benzene
2023-02-15 14:27:18 +08:00
类似 @timethinker 的说法,如果自己写 SQL 的话,你 INSERT 的时候不指定相应字段,它就会自动插入默认值。如果你写了字段,MyBatis 填充 VALUES 时又用的是 null 的话,就会出现你说的问一句。
wangxin3
2023-02-15 14:40:19 +08:00
用 mybatis 的话 只能 xml 中自己写动态 SQL ,另外不建议在实体类中定义默认值(很坑的);如果可以替换用 mybatisplus 的话字段上有个注解 @TableField(insertStrategy = FieldStrategy.NOT_NULL)也可帮到你。
mosliu
2023-02-15 15:30:23 +08:00
entity 中的 field 赋处置就行了。。
问题就是看上去不优雅。。
Aresxue
2023-02-20 15:49:55 +08:00
数据库里不要用 not null 设个 default ''好了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/916162

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX