当我们想修改数据时,想根据id或者其他的条件只修改对象的某几个字段而不想修改所有字段,下面这种写法简单且高效
LambdaUpdateWrapper<DataLayoutModuleInfo> wrapperHistory = Wrappers.<DataLayoutModuleInfo>update().lambda()
.in(DataLayoutModuleInfo::getModuleCode, moduleCodeList)
.set(DataLayoutModuleInfo::getStatus, Status.DELETE.getCode())
.set(DataLayoutModuleInfo::getUpdateUser, userName);
// baseMapper写法
baseMapper.update(null, wrapperHistory);
// service写法
this.update(wrapperHistory);
其中updateWrapper中.in(DataLayoutModuleInfo::getModuleCode, moduleCodeList)为条件
set为你想要修改的字段和修改后的值
其中Service层的写法和baseMapper写发稍有不同,底层Service也是调用的baseMapper
源码如下:
default boolean update(Wrapper<T> updateWrapper) {
return this.update((Object)null, updateWrapper);
}
default boolean update(T entity, Wrapper<T> updateWrapper) {
return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
}