一、背景描述
項(xiàng)目技術(shù)棧:jdk (1.8) + spring boot (2.1.0) + mybatis-plus (3.5.1)
數(shù)據(jù)庫: MySQL
字段類型:varchar 和 Integer
從前端傳過來的數(shù)據(jù)實(shí)體字段, convertType 和 step 設(shè)為null時(shí),使用mybatis-plus 的 updateById方法往數(shù)據(jù)庫中更新時(shí),這兩個(gè)字段不更新,數(shù)據(jù)不為空可以插入。
二、問題原因
mybatis-plus在更新的時(shí)候做了null判斷,默認(rèn)不更新為null的傳參。
三、解決方案
有兩種方法都可以解決這個(gè)問題,都是加注解
3.1 @TableField(fill = FieldFill.UPDATE)
在需要更新的字段上加上 mybatis plus 的注解 @TableField(fill = FieldFill.UPDATE) ,它的作用是字段填充時(shí)要更新此字段文章來源:http://www.zghlxwxcb.cn/news/detail-792716.html
@ApiModelProperty("轉(zhuǎn)換值")
@TableField(fill = FieldFill.UPDATE)
private String convertValue;
@ApiModelProperty("步長")
@TableField(fill = FieldFill.UPDATE)
private Integer step;
3.2 @TableField(updateStrategy = FieldStrategy.IGNORED)
在實(shí)體類對應(yīng)字段上添加上這個(gè)注解,它的作用是忽略NULL值的判斷文章來源地址http://www.zghlxwxcb.cn/news/detail-792716.html
@ApiModelProperty("轉(zhuǎn)換值")
@TableField(updateStrategy = FieldStrategy.IGNORED)
private String convertValue;
@ApiModelProperty("步長")
@TableField(updateStrategy = FieldStrategy.IGNORED)
private Integer step;
到了這里,關(guān)于【Mybatis-Plus】mybatisplus更新時(shí),實(shí)體字段為空,數(shù)據(jù)庫不更新的解決方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!