重构批量删除.
This commit is contained in:
parent
486e3b84de
commit
95ae900334
@ -17,11 +17,20 @@ package com.baomidou.mybatisplus.core.injector.methods;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
import com.baomidou.mybatisplus.core.enums.SqlMethod;
|
||||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
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.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 com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||||
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.MappedStatement;
|
||||||
import org.apache.ibatis.mapping.SqlSource;
|
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 集合删除
|
* 根据 ID 集合删除
|
||||||
*
|
*
|
||||||
@ -68,11 +77,22 @@ public class DeleteBatchByIds extends AbstractMethod {
|
|||||||
* @since 3.5.0
|
* @since 3.5.0
|
||||||
*/
|
*/
|
||||||
public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) {
|
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(),
|
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())",
|
SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
|
||||||
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
|
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
|
||||||
COLL, null, "item", COMMA),
|
COLL, null, "item", COMMA),
|
||||||
tableInfo.getLogicDeleteSql(true, true));
|
tableInfo.getLogicDeleteSql(true, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,6 @@ import org.apache.ibatis.session.SqlSession;
|
|||||||
import org.apache.ibatis.session.SqlSessionFactory;
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -199,19 +198,10 @@ public interface BaseMapper<T> extends Mapper<T> {
|
|||||||
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
|
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
|
||||||
Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
|
Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
|
||||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
|
||||||
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));
|
|
||||||
}
|
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
if (useFill && tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
|
||||||
|
params.put(Constants.ENTITY, tableInfo.newInstance());
|
||||||
|
}
|
||||||
params.put(Constants.COLL, collections);
|
params.put(Constants.COLL, collections);
|
||||||
return sqlSession.delete(mapperInterface.getName() + StringPool.DOT + SqlMethod.DELETE_BATCH_BY_IDS.getMethod(), params);
|
return sqlSession.delete(mapperInterface.getName() + StringPool.DOT + SqlMethod.DELETE_BATCH_BY_IDS.getMethod(), params);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user