Skip to content

Commit

Permalink
重构批量删除.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Jun 8, 2024
1 parent 486e3b8 commit 95ae900
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.List;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

/**
* 根据 ID 集合删除
*
Expand Down Expand Up @@ -68,11 +77,22 @@ public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> mode
* @since 3.5.0
*/
public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) {
List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
.filter(TableFieldInfo::isWithUpdateFill)
.filter(f -> !f.isLogicDelete())
.collect(toList());
String sqlSet = "SET ";
if (CollectionUtils.isNotEmpty(fieldInfos)) {
sqlSet += SqlScriptUtils.convertIf(fieldInfos.stream()
.map(i -> i.getSqlSet(Constants.ENTITY + StringPool.DOT)).collect(joining(EMPTY)), String.format("%s != null", Constants.ENTITY), true);
}
sqlSet += StringPool.EMPTY + tableInfo.getLogicDeleteSql(false, false);
return String.format(sqlMethod.getSql(), tableInfo.getTableName(),
sqlLogicSet(tableInfo), tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach(
sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach(
SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
COLL, null, "item", COMMA),
tableInfo.getLogicDeleteSql(true, true));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -199,19 +198,10 @@ default int deleteByIds(@Param(Constants.COLL) Collection<?> collections, boolea
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Map<String, Object> params = new HashMap<>();
if (useFill && tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
List<Object> ids = new ArrayList<>(collections.size());
for (Object obj : collections) {
// TODO 乐观锁待定(感觉都要删除了,可以不用考虑乐观锁的情况了)...
if (entityClass.isAssignableFrom(obj.getClass())) {
ids.add(tableInfo.getPropertyValue(obj, tableInfo.getKeyProperty()));
} else {
ids.add(obj);
}
}
return this.update(tableInfo.newInstance(), Wrappers.<T>update().in(tableInfo.getKeyColumn(), ids));
params.put(Constants.ENTITY, tableInfo.newInstance());
}
Map<String, Object> params = new HashMap<>();
params.put(Constants.COLL, collections);
return sqlSession.delete(mapperInterface.getName() + StringPool.DOT + SqlMethod.DELETE_BATCH_BY_IDS.getMethod(), params);
}
Expand Down

0 comments on commit 95ae900

Please sign in to comment.