当mybatisPlus与tk.mybatis遇到更新

前言

      今天其实也没啥好说的,最近都是在赶工期CRUD,没有时间也没有场景做技术提升。今天主要是来说下Mybatis的ORM框架在做数据更新时的对比。mybatisPlus与tk.mybatis的更新操作就通过场景比较吧。


一、更新场景

      字段有值,现在想更新为null。

二、两个更新操作的对比

1.mybatisPlus的更新

int updateById(@Param("et") T entity);

这个没什么好解释的吧,就是实体对象又id,然后其他字段有值,做更新。

int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
dangerousInfo.setModifyTime(DateUtil.date());
//this.baseMapper.updateById(dangerousInfo);
UpdateWrapper<DangerousInfo> updateWrapper = new UpdateWrapper();
updateWrapper.eq("id",id);
//updateWrapper.eq("delete_state",dangerousInfo.getDeleteState());
updateWrapper.set("plan_start_time",planStartTime);
updateWrapper.set("plan_end_time",planEndTime);
updateWrapper.set("actual_start_time",dangerousInfo.getActualStartTime());
updateWrapper.set("actual_end_time",dangerousInfo.getActualEndTime());
this.baseMapper.update(dangerousInfo,updateWrapper);

      这里原先用的就是updateById方法,但是实际上能,几个时间如果要更新为null,用updateById就无法实现,所以改成下面的方式。
      实际上UpdateWrapper对象很特殊,可以设置条件,也可以设置set内容。


2.tk.mybatis

@UpdateProvider(type = BaseUpdateProvider.class, method = “dynamicSQL”)
int updateByPrimaryKeySelective(T record);
      这种方式更新不会更新入参为空的字段。

@UpdateProvider(type = BaseUpdateProvider.class, method = “dynamicSQL”)
int updateByPrimaryKey(T record);
      这种方式更新,为空的字段也会更新,也就是sql会出现 xxx = null。


总结

  • mybatisPlus如果要更新null,用updateById也是可以实现的,就是在相应字段上加上
    @TableField(updateStrategy = FieldStrategy.IGNORED)注解,或者全局设置。
    ignored:“忽略判断”,所有字段都更新和插入。
    not_null:“非 null 判断”,只更新和插入非null值。
    not_empty:“非空判断”, 只更新和插入非null值且非空字符串。
    default: 默认的,一般只用于注解里。
    这种不管是全局配置、还是字段注解都不是我所喜欢的。似乎这一切在框架搭建开始就定死了,要么选择全字段传参,要么实体加注解。但是这些后期都不能随意改动了,你懂的。

  • tk.mybatis的更新方式其实还有很多,比如:
    @UpdateProvider(type = ConditionProvider.class, method = “dynamicSQL”)
    int updateByCondition(@Param(“record”) T record, @Param(“example”) Object condition);
    @UpdateProvider(type = ConditionProvider.class, method = “dynamicSQL”)
    int updateByConditionSelective(@Param(“record”) T record, @Param(“example”) Object condition);
    等等。

  • tk.mybatis的更新就跟配置解耦,完全看使用的更新方式。也就是看业务需要,同时在更新时还可以设置忽略字段。这种细粒度的我更喜欢,后面来人编码也不会受到影响。
    针对更新这点,决定用mybatisPlus还是tk.mybatis,那就看各位了。这都是比较好的两个ORM框架,我也没资格说哪个更好。UPing

猜你喜欢

转载自blog.csdn.net/zwrlj527/article/details/126690359
今日推荐