当前使用版本(必填,否则不予处理)
com.baomidou:mybatis-plus-boot-starter:3.3.1.tmp
该问题是如何引起的?(确定最新版也有问题再提!!!)
设置了逻辑删除字段时,在update中会忽略掉逻辑字段的set,是否可以使用配置的方式设置是否需要在update中忽略逻辑删除字段。 例如我的表有id,name,deleted,其中deleted是逻辑删除字段,那么现在的update的sql是: update table set id=x, name=x where deleted=0; 是否可添加配置,将sql变为update table set id=x, name=x,deleted=x(1) where deleted=0;
有这种需求的原因是,加入我有一组entity,其中部分被标记为deleted,部分是新增,部分是更新,那么我只需要调用saveOrUpdate就可以了,不然的话,得首先挑出来被标记为deleted的记录,调用delete方法,其余的调用saveOrUpdate方法,不太方便
Comment From: iHearty
目前我的解决方法时自定义了SqlInjector, 重写了Update和UpdateById两个method,自定义的这两个Update方法不会忽略logic字段。自己添加了一个配置属性用来控制使用原有的Update方法还是自定义的Update方法。 希望官方能添加此配置。
Comment From: miemieYaho
用 updatewrapper.set
Comment From: iHearty
我是使用的batch的sqlsession的update方法,代码如下: debug发下,获取的sql还是从sqlsource上拿的,所以逻辑删除字段依旧是没有的。
List<Serializable> ids = entityList.stream()
.map(entity -> (Serializable) ReflectionKit.getMethodValue(cls, entity, keyProperty))
.filter(Objects::nonNull)
.collect(Collectors.toList());
Map<Serializable, T> existed = ids.size() > 0 ? listByIds(ids).stream()
.collect(Collectors.toMap(entity -> (Serializable) ReflectionKit.getMethodValue(cls, entity, keyProperty),
entity -> entity)) : Collections.emptyMap();
try(SqlSession batchSqlSession = sqlSessionBatch()) {
int i = 0;
for(T entity : entityList) {
Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty);
if(ObjectUtils.isEmpty(idVal) || Objects.isNull(existed.get(idVal))) {
batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity);
}
else {
MapperMethod.ParamMap<Object> param = new MapperMethod.ParamMap<>();
param.put(Constants.ENTITY, entity);
if(tableInfo.isLogicDelete()) {
TableFieldInfo field = tableInfo.getFieldList()
.stream()
.filter(TableFieldInfo::isLogicDelete)
.findFirst()
.orElseThrow(() -> ExceptionUtils.mpe("can't find the logicFiled from table {%s}",
tableInfo.getTableName()));
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.set(field.getColumn(), ReflectionKit.getMethodValue(cls, entity, field.getProperty()));
param.put(Constants.WRAPPER, updateWrapper);
}
batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
}
if(i >= 1 && i % batchSize == 0) {
batchSqlSession.flushStatements();
}
i++;
}
batchSqlSession.flushStatements();
}
Comment From: ChinaWim
@iHearty 我也遇到这个问题,想知道你怎么配置的,代码能发出来看看吗