add 新增过滤处理器使用

This commit is contained in:
AprilWind 2025-08-03 16:22:53 +08:00
parent a58caadbaa
commit 3db7f8ce74
4 changed files with 33 additions and 19 deletions

View File

@ -15,12 +15,15 @@ import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.logging.LogFactory;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.mybatis.handler.FilterResultHandler;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
/** /**
* 自定义 Mapper 接口, 实现 自定义扩展 * 自定义 Mapper 接口, 实现 自定义扩展
@ -355,4 +358,34 @@ public interface BaseMapperPlus<T, V> extends BaseMapper<T> {
return StreamUtils.toList(this.selectObjs(wrapper), mapper); return StreamUtils.toList(this.selectObjs(wrapper), mapper);
} }
/**
* 根据查询条件和过滤条件查询数据并将结果转换为指定的VO类型列表
*
* @param wrapper 查询条件Wrapper
* @param filter 结果过滤条件
* @return 查询到的符合条件的对象列表经过转换为指定类型的对象后返回
*/
default List<V> selectFilterVoList(Wrapper<T> wrapper, Predicate<T> filter) {
return this.selectFilterVoList(wrapper, filter, this.currentVoClass());
}
/**
* 根据查询条件和过滤条件查询数据并将结果转换为指定的VO类型列表
*
* @param wrapper 查询条件Wrapper
* @param filter 结果过滤条件
* @param voClass 要转换的VO类的Class对象
* @param <C> VO类的类型
* @return 查询到的符合条件的对象列表经过转换为指定类型的对象后返回
*/
default <C> List<C> selectFilterVoList(Wrapper<T> wrapper, Predicate<T> filter, Class<C> voClass) {
List<T> list = new ArrayList<>();
// 使用过滤处理器过滤数据并将符合条件的结果加入 list
this.selectList(wrapper, new FilterResultHandler<>(filter, list::add));
if (CollUtil.isEmpty(list)) {
return CollUtil.newArrayList();
}
return MapstructUtils.convert(list, voClass);
}
} }

View File

@ -59,16 +59,8 @@ public class BatchResultHandler<T> implements ResultHandler<T> {
* *
* @param batchConsumer 批量消费函数不能为null * @param batchConsumer 批量消费函数不能为null
* @param batchSize 批量大小必须大于0 * @param batchSize 批量大小必须大于0
* @throws NullPointerException batchConsumer为null时抛出
* @throws IllegalArgumentException batchSize小于等于0时抛出
*/ */
public BatchResultHandler(Consumer<List<T>> batchConsumer, int batchSize) { public BatchResultHandler(Consumer<List<T>> batchConsumer, int batchSize) {
if (batchConsumer == null) {
throw new NullPointerException("batchConsumer不能为null");
}
if (batchSize <= 0) {
throw new IllegalArgumentException("batchSize必须大于0");
}
this.batchConsumer = batchConsumer; this.batchConsumer = batchConsumer;
this.batchSize = batchSize; this.batchSize = batchSize;
this.buffer = new ArrayList<>(batchSize); this.buffer = new ArrayList<>(batchSize);

View File

@ -35,15 +35,8 @@ public class FilterResultHandler<T> implements ResultHandler<T> {
* *
* @param filter 过滤条件 * @param filter 过滤条件
* @param consumer 满足过滤条件时的处理函数 * @param consumer 满足过滤条件时的处理函数
* @throws NullPointerException 如果 filter consumer null
*/ */
public FilterResultHandler(Predicate<T> filter, Consumer<T> consumer) { public FilterResultHandler(Predicate<T> filter, Consumer<T> consumer) {
if (filter == null) {
throw new NullPointerException("过滤条件,不能为空");
}
if (consumer == null) {
throw new NullPointerException("处理函数,不能为空");
}
this.filter = filter; this.filter = filter;
this.consumer = consumer; this.consumer = consumer;
} }

View File

@ -25,12 +25,8 @@ public class SimpleResultHandler<T> implements ResultHandler<T> {
* 构造 SimpleResultHandler 实例 * 构造 SimpleResultHandler 实例
* *
* @param consumer 处理单条查询结果的消费函数不能为空 * @param consumer 处理单条查询结果的消费函数不能为空
* @throws NullPointerException 如果 consumer null 抛出
*/ */
public SimpleResultHandler(Consumer<T> consumer) { public SimpleResultHandler(Consumer<T> consumer) {
if (consumer == null) {
throw new NullPointerException("consumer不能为null");
}
this.consumer = consumer; this.consumer = consumer;
} }