封装的多线程查询工具,需要依赖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);
相关推荐
Fireworkitte3 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083163 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT4 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.4 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超4 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice4 小时前
对象的finalization机制Test
java·开发语言·jvm
望获linux5 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件
JosieBook6 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea
Thomas_YXQ6 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
喜欢敲代码的程序员6 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis