封装的多线程查询工具,需要依赖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;
    }
}

二、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);

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-06 05:54:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-06 05:54:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-06 05:54:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-06 05:54:04       20 阅读

热门阅读

  1. 08、docker pull nacos/nacos-server慢解决方案

    2024-01-06 05:54:04       36 阅读
  2. 且看迥然不同的 diff

    2024-01-06 05:54:04       40 阅读
  3. office学习记录

    2024-01-06 05:54:04       42 阅读
  4. LeetCode 141

    2024-01-06 05:54:04       39 阅读
  5. tf-idf +逻辑回归来识别垃圾文本

    2024-01-06 05:54:04       38 阅读
  6. easycode 插件配置文件

    2024-01-06 05:54:04       33 阅读
  7. Linux上创建IntelliJ IDEA的快捷方式

    2024-01-06 05:54:04       30 阅读