mybatisplus 强制制空 空覆盖原来的字符串
??
方法一
@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("base_party_member") @ApiModel(value="BasePartyMember對象", description="黨員管理表") public class BasePartyMember extends Model<BasePartyMember> {private static final long serialVersionUID=1L;@ApiModelProperty(value = "黨員id")@TableId(value = "id", type = IdType.AUTO)private Long id;@ApiModelProperty(value = "服務(wù)范圍 可關(guān)聯(lián)多個(gè)房屋")@TableField(fill = FieldFill.UPDATE)//@TableField(value = "service_area_ids",fill = FieldFill.UPDATE)//@TableField(value = "service_area_ids", updateStrategy = FieldStrategy.IGNORED,jdbcType = VARCHAR )private String serviceAreaIds;@ApiModelProperty(value = "是否刪除,Y-刪除,N-未刪除")@TableField("is_deleted")private String isDeleted;@ApiModelProperty(value = "創(chuàng)建時(shí)間")@TableField("create_time")private Date createTime;@ApiModelProperty(value = "創(chuàng)建人")@TableField("creator")private Long creator;@ApiModelProperty(value = "最后修改人")@TableField("operator")private Long operator;@ApiModelProperty(value = "最后修改時(shí)間")@TableField("update_time")private Date updateTime;@Overrideprotected Serializable pkVal() {return this.id;}}?
count = basePartyMemberMapper.updateById(newPartyMember); if(StringUtils.isBlank(newPartyMember.getServiceAreaIds())){//也可以參考下面這種寫法basePartyMemberMapper.update(null,Wrappers.<BasePartyMember>lambdaUpdate().eq(BasePartyMember::getId,newPartyMember.getId()).set(BasePartyMember::getServiceAreaIds, "")); }?
?
2021-05-08 11:04:22.576 ERROR 1 [http-nio-18093-exec-3] c.w.s.c.c.ExceptionHandleConfiguration ? : [500]_
### Error updating database. ?Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'service_area_ids' cannot be null
### The error may exist in com/wochanye/ssc/party/mapper/BasePartyMemberMapper.java (best guess)
### The error may involve com.wochanye.ssc.party.mapper.BasePartyMemberMapper.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE base_party_member ?SET service_area_ids=? ? ? ?WHERE (id = ?)
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'service_area_ids' cannot be null
; Column 'service_area_ids' cannot be null; nested exception is java.sql.SQLIntegrityConstraintViolationException: Column 'service_area_ids' cannot be null
org.springframework.dao.DataIntegrityViolationException:?
### Error updating database. ?Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'service_area_ids' cannot be null
### The error may exist in com/wochanye/ssc/party/mapper/BasePartyMemberMapper.java (best guess)
### The error may involve com.wochanye.ssc.party.mapper.BasePartyMemberMapper.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE base_party_member ?SET service_area_ids=? ? ? ?WHERE (id = ?)
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'service_area_ids' cannot be null
; Column 'service_area_ids' cannot be null; nested exception is java.sql.SQLIntegrityConstraintViolationException: Column 'service_area_ids' cannot be null
? ? ? ? at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:87)
?
?
方法二
https://blog.csdn.net/qianlingo/article/details/105120219
@Data
@TableName("shop_item")
public class ShopItem implements Serializable {
?
?
? ? ?private static final long serialVersionUID = 1L;
?
? ? /**
? ? ?* 編號
? ? ?*/
? ? @TableId(type= IdType.INPUT)
? ? private String id;
?
? ? /**
? ? ?* 物品名稱
? ? ?*/
? ? private String itemName;
?
? ? /**
? ? ?* 物品價(jià)格
? ? ?*/
? ? @TableField(fill = FieldFill.UPDATE)
? ? private Double itemPrice;
?
? ? /**
? ? * ? ?添加人編號
? ? */
? ? private String addUserId;
?
? ? /**
? ? * ? ?添加時(shí)間
? ? */
? ? private Date addTime;
?
}
我們在itemPrice屬性的頂上加上@TableField(fill = FieldFill.UPDATE)后,在執(zhí)行一遍修改方法看看!
update?
? ? shop_item?
set?
? ? item_name = 'iPhone 8 Plus',
?
? ? item_price = ''
where?
id = '361E8C48-6699-4ED5-83C4-7C9D98747C2C';
bingo,成功!
————————————————
版權(quán)聲明:本文為CSDN博主「qianlingo」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qianlingo/article/details/105120219
?
?
?
方法三
https://blog.csdn.net/l848168/article/details/92829930
?
field-strategy字段更新插入策略屬性說明:
IGNORED(0): "忽略判斷", 所有字段都更新和插入?
NOT_NULL(1): "非 NULL 判斷", 只更新和插入非NULL值 ?
NOT_EMPTY(2): "非空判斷", 只更新和插入非NULL值且非空字符串 ? ??
DEFAULT:默認(rèn)NOT_NULL?
?
?
方法五
https://www.cnblogs.com/wuxixin/p/13573624.html
最近在工作的時(shí)候遇到使用mybatis-plus框架遇到一個(gè)無法更新空字符串和null的問題,看了很多博客文章,都沒有解決掉,在此記錄一下自己解決方式
xxxx.updateById(user)一般都是調(diào)用?updateById()這個(gè)方法導(dǎo)致無法把null或者空字符串更新到數(shù)據(jù)庫中的。
通過查看mybatis-plus官方的api文檔也記錄了對應(yīng)的解決方法,一共有三種解決的方法
插入或更新的字段有 空字符串 或者?null
第一種方式,對自己的系統(tǒng)影響有點(diǎn)大,不是很適合,果斷放棄了,沒有使用
第二種方式,是實(shí)體類的需要傳入null或者是空字符的屬性中調(diào)整驗(yàn)證注解,但是只加?strategy = FieldStrategy.IGNORED 是不行的,會報(bào)錯(cuò),是因?yàn)闆]有指定該字段的jdbcType的類型,加上就可以解決掉
?
?
1 @TableField(strategy = FieldStrategy.IGNORED,el = "relationAccId,jdbcType=VARCHAR") 2 private String relationAccId;?
第三種方式,mybatis-plus的版本必須3.x以上才可以用,使用UpdateWrapper來更新
//updateAllColumnById(entity) // 全部字段更新: 3.0已經(jīng)移除 mapper.update(new User().setName("mp").setAge(3),Wrappers.<User>lambdaUpdate().set(User::getEmail, null) //把email設(shè)置成null.eq(User::getId, 2) ); //也可以參考下面這種寫法 mapper.update(null,Wrappers.<User>lambdaUpdate().set(User::getAge, 3).set(User::getName, "mp").set(User::getEmail, null) //把email設(shè)置成null.eq(User::getId, 2) );還是官方API靠譜,遇到問題,可以先到官方網(wǎng)址查查API,可能是個(gè)不錯(cuò)的選擇!
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的mybatisplus 强制制空 空覆盖原来的字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stream 提取某字段_java8从l
- 下一篇: nacos 读取纯数字字符 出错