多线程解决大数据批量导出问题(demo)

2023-12-13 13:23:16

1.首先从网上找一个到工具类,我这里是ExcelUtils,如下

?

package com.org.util;

import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class ExcelUtils {
? // 导出 Excel
    public static Integer exportExcel(SXSSFSheet sheet, List<Object> dataList, Integer currentRow) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        if (null == dataList || dataList.size() == 0) {
            return -1;
        }

        // 反射
        Object object =  dataList.get(0);
        Class<?> clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();

?       //如果是第一行,创建表头
        if (0 == currentRow) {
            SXSSFRow row = sheet.createRow(currentRow++);
            for (int i = 0; i < fields.length; i++) {
                SXSSFCell cell = row.createCell(i);
                cell.setCellValue(fields[i].getName());//设置单元格的值为字段名
            }
        }
        //填充数据
        for (Object item : dataList) {
            SXSSFRow row = sheet.createRow(currentRow++);
            for (int i = 0; i < fields.length; i++) {
                SXSSFCell cell = row.createCell(i);
                // get Method
            PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fields[i].getName(), item.getClass());
                Method getMethod = propertyDescriptor.getReadMethod();
                // value
                Object value = getMethod.invoke(item);
                cell.setCellValue(value.toString());//设置单元格的值为字段值的字符表现形式
            }
        }
        return currentRow;
    }
}

public class DataBaseTest {

    private static final ConcurrentHashMap<Integer, FutureTask<Integer>> taskList = new ConcurrentHashMap<>();

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
        // mapper
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 查出总数
        int totalNum = userMapper.getTotalNum();
        // 10个线程数
        int taskNum = 10;
        // 总数据量
        int avg = (int) Math.floor(totalNum / taskNum);
        int[] startRows = calcStartRowIndex(totalNum, taskNum);
        // Excel
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        SXSSFSheet sheet = workbook.createSheet();

        for (int i = 0; i < taskNum; i++) {
            final int temp = i;
            FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    System.out.println("开始执行任务" + temp + "...");
                    HashMap<String, Object> map = new HashMap<>();
                    List<User> userList = null;
                    // 分页查询
                    if (temp == taskNum - 1) {
                        // 如果是最后一个线程
                        map.put("startIndex", startRows[temp] - 1); //9001-1
                        // 计算起始位置到最后的数量
                        map.put("num", totalNum - startRows[temp] + 1); //10001-9001+1
                        userList = userMapper.getListByPage(map);
                    } else {
                        //如果是前九个线程的花执行下面的操作
                        map.put("startIndex", startRows[temp] == 0 ? 0 : startRows[temp] - 1);
                        map.put("num", avg);
                        userList = userMapper.getListByPage(map);
                    }
                    List<Object> dataList = new ArrayList<>();
                    for (User user : userList) {
                        dataList.add(user);
                    }
                    Integer currentRow = ExcelUtils.exportExcel(sheet, dataList, startRows[temp]);
                    System.out.println("任务" + temp + "执行结束...");
                    return currentRow;
                }
            });
            taskList.putIfAbsent(i, futureTask);
        }

        int taskIndex = 0;
        while (true) {
            FutureTask<Integer> futureTask = taskList.remove(taskIndex++);
            if (null != futureTask) {
                futureTask.run();
                Integer currentRow = futureTask.get();
            }
            if (taskList.size() == 0) {
                // 写入磁盘
                System.out.println("开始写入磁盘");
                FileOutputStream fileOutputStream = new FileOutputStream(new File("c:\\users\\che\\desktop\\test01.xlsx"));
                workbook.write(fileOutputStream);
                break;
            }
        }
    }

    // 计算每一趟起始位置
    public static int[] calcStartRowIndex(int totalNum, int taskNum) {

        int[] resultArray = new int[taskNum];
        int avg = (int) Math.floor(totalNum / taskNum);
        for (int i = 0; i < taskNum; i++) {
            if (i == 0) {
                resultArray[i] = i * avg;
            } else {
                resultArray[i] = i * avg + 1;
            }
        }
        return resultArray;
    }

    public static void addDataSource() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            for (int i = 0; i < 10000; i++) {
                User user = new User("T" + i, i, "男", "10086", "qwe@qq.com", "日本京都");
                System.out.println(user);
                userMapper.addUser(user);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            sqlSession.close();
        }
    }
}

具体代码示例包在次处

文章来源:https://blog.csdn.net/qq_42362007/article/details/134551052
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。