封装的多线程查询工具,需要依赖mybatis-plus开启线程池

文章目录


一、MdUtils

java 复制代码
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

java 复制代码
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 + ";"));
    }
}

三、调用

java 复制代码
例如:List<ActEamRepairOrder> actEamRepairOrders = doPackageResCallables(ActEamRepairOrderService.class, new LambdaQueryWrapper<ActEamRepairOrder>(), threadNumber,ActEamRepairOrder::getOrderId,ActEamRepairOrder::getOrderName);
相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸4 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了5 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
jokerest1235 小时前
web——sqliabs靶场——第十三关——报错注入+布尔盲注
mybatis
小二·5 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王6 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康6 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式