本文介绍: 【代码】封装的多线程查询工具,需要依赖mybatis-plus开启线程池。
文章目录
一、MdUtils
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import com.eam.common.utils.spring.SpringUtils;
import lombok.SneakyThrows;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
public class MdUtils {
/**
*
* @param clz 查询的isvc
* @param wrapper 查询的wrapper
* @param i 线程类数
* @param columns 需要查询的字段
*/
@SafeVarargs
public static <T>List<T> doPackageResCallables(Class<? extends IService<T>> clz, LambdaQueryWrapper<T> wrapper, Long i, SFunction<T, ?>... columns){
List<MdCallable<T>> mdCallables = packageCallables(clz, wrapper,i,columns);
return executeTask(mdCallables);
}
@SafeVarargs
public static <T>List<T> doPackageResCallables(Class<? extends IService<T>> clz,LambdaQueryWrapper<T> wrapper,SFunction<T, ?>... columns){
List<MdCallable<T>> mdCallables = packageCallables(clz, wrapper,columns);
return executeTask(mdCallables);
}
@SafeVarargs
public static <T>List<MdCallable<T>> packageCallables(Class<? extends IService<T>> clz,LambdaQueryWrapper<T> wrapper, SFunction<T, ?>... columns){
return packageCallables(clz,wrapper,200L,columns);
}
@SafeVarargs
public static <T>List<MdCallable<T>> packageCallables(Class<? extends IService<T>> clz,LambdaQueryWrapper<T> wrapper,Long i, SFunction<T, ?>... columns){
IService<T> bean = SpringUtils.getBean(clz);
long count = bean.count();
i = (Objects.isNull(i) || 0 == i) ? 10 : count>i?i:count;
Long pageSize = count / i;
List<MdCallable<T>> ts = new ArrayList<>();
for (Long l = 0L; l < i; l++) {
ts.add(new MdCallable<T>(pageSize*l,pageSize,clz,wrapper,columns));
}
return ts;
}
@SneakyThrows
@SuppressWarnings("ALL")
public static <T>List<T> executeTask(List<MdCallable<T>> c){
List<Future<T>> priceFutureList = new ArrayList<>();
ArrayList<T> res = new ArrayList<>();
c.forEach(tCallable -> priceFutureList.add(SpringUtils.getBean(ThreadPoolTaskExecutor.class).submit((Callable<T>) tCallable)));
for (Future<T> f : priceFutureList) {
res.addAll((List<T>) f.get());
}
return res;
}
//获取结果集
public static<T> List<T> getFotFutureList(Class<T> clz,List l){
for (Object o : l) {
if (o instanceof List && !((List<?>) o).isEmpty() && ((List<?>) o).get(0).getClass().equals(clz)){
return (List<T>) o;
}
}
return null;
}
//异步获取任务
@SafeVarargs
public static <U>List<U> doCompletableFuture(Supplier<U>... supplier){
List<CompletableFuture<U>> collect = new ArrayList<>(Arrays.asList(supplier)).stream().map(supplierSig -> CompletableFuture.supplyAsync(supplierSig, SpringUtils.getBean(ThreadPoolTaskExecutor.class))).collect(Collectors.toList());
CompletableFuture.allOf(collect.toArray(new CompletableFuture[0])).join();
return collect.stream().map(CompletableFuture::join).collect(Collectors.toList());
}
}
二、MdCallable
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import com.eam.common.utils.spring.SpringUtils;
import lombok.AllArgsConstructor;
import java.util.List;
import java.util.concurrent.Callable;
@AllArgsConstructor
public class MdCallable<T> implements Callable<List<T>> {
Long limit0;
Long limit1;
Class<? extends IService<T>> clz;
LambdaQueryWrapper<T> wrapper;
SFunction<T, ?>[] columns;
@Override
public List<T> call() throws Exception {
return SpringUtils.getBean(clz).list(wrapper.select(columns).last(" LIMIT " +limit0 + "," + limit1 + ";"));
}
}
三、调用
例如:List<ActEamRepairOrder> actEamRepairOrders = doPackageResCallables(ActEamRepairOrderService.class, new LambdaQueryWrapper<ActEamRepairOrder>(), threadNumber,ActEamRepairOrder::getOrderId,ActEamRepairOrder::getOrderName);
原文地址:https://blog.csdn.net/weixin_45479938/article/details/135412655
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_52638.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。