mybatisplus 强制制空 空覆盖原来的字符串

@ApiModelProperty(value ="证件照片url")
@TableField(value = "id_photo_url",fill = FieldFill.UPDATE)
private String idPhotoUrl;

  

方法一

@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 = "服务范围 可关联多个房屋")
    @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 = "创建时间")
    @TableField("create_time")
    private Date createTime;

    @ApiModelProperty(value = "创建人")
    @TableField("creator")
    private Long creator;

    @ApiModelProperty(value = "最后修改人")
    @TableField("operator")
    private Long operator;

    @ApiModelProperty(value = "最后修改时间")
    @TableField("update_time")
    private Date updateTime;


    @Override
    protected 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;
 
    /**
     * 物品价格
     */
    @TableField(fill = FieldFill.UPDATE)
    private Double itemPrice;

 
    /**
    *    添加人编号
    */
    private String addUserId;
 
    /**
    *    添加时间
    */
    private Date addTime;
 
}
我们在itemPrice属性的顶上加上@TableField(fill = FieldFill.UPDATE)后,在执行一遍修改方法看看!

update 
    shop_item 
set 
    item_name = 'iPhone 8 Plus',
 
    item_price = ''
where 
id = '361E8C48-6699-4ED5-83C4-7C9D98747C2C';
bingo,成功!
————————————————
版权声明:本文为CSDN博主「qianlingo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接: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:默认NOT_NULL 

 

 

方法五

https://www.cnblogs.com/wuxixin/p/13573624.html

最近在工作的时候遇到使用mybatis-plus框架遇到一个无法更新空字符串和null的问题,看了很多博客文章,都没有解决掉,在此记录一下自己解决方式

xxxx.updateById(user)

一般都是调用 updateById()这个方法导致无法把null或者空字符串更新到数据库中的。

通过查看mybatis-plus官方的api文档也记录了对应的解决方法,一共有三种解决的方法

插入或更新的字段有 空字符串 或者 null

第一种方式,对自己的系统影响有点大,不是很适合,果断放弃了,没有使用

第二种方式,是实体类的需要传入null或者是空字符的属性中调整验证注解,但是只加 strategy = FieldStrategy.IGNORED 是不行的,会报错,是因为没有指定该字段的jdbcType的类型,加上就可以解决掉

 

 

1 @TableField(strategy = FieldStrategy.IGNORED,el = "relationAccId,jdbcType=VARCHAR")
2 private String relationAccId;

 

第三种方式,mybatis-plus的版本必须3.x以上才可以用,使用UpdateWrapper来更新

复制代码

//updateAllColumnById(entity) // 全部字段更新: 3.0已经移除
mapper.update(
   new User().setName("mp").setAge(3),
   Wrappers.<User>lambdaUpdate()
           .set(User::getEmail, null) //把email设置成null
           .eq(User::getId, 2)
);
//也可以参考下面这种写法
mapper.update(
    null,
    Wrappers.<User>lambdaUpdate()
       .set(User::getAge, 3)
       .set(User::getName, "mp")
       .set(User::getEmail, null) //把email设置成null
       .eq(User::getId, 2)
);

复制代码

还是官方API靠谱,遇到问题,可以先到官方网址查查API,可能是个不错的选择!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值