zoukankan      html  css  js  c++  java
  • 【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)

    前文里谈过一次性从数据库取一个大结果集有可能导致outofMemory,当时的想法是分批去取回来,今天把它实现了,特地把代码分享出来:

    工程下载:https://files.cnblogs.com/files/xiandedanteng/CsvDownload20191027.rar

     生成CSV文件的三个不同函数

    package com.hy.csvdld.util;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.log4j.Logger;
    
    import com.hy.csvdld.Entity.Emp;
    import com.hy.csvdld.service.EmpService;
    
    // 用于生成CSV文件
    public class CsvMaker {
        private static Logger log = Logger.getLogger(CsvMaker.class);
        
        public void makeTenCsv(File file, EmpService empService) {
            try {
                List<Emp> emps = empService.selectTenEmp();
    
                FileWriter fileWriter = new FileWriter(file, true);
    
                int index = 0;
                for (Emp emp:emps) {
                    index++;
    
                    String info =""+index+","+ emp.asCsvLine()+ System.getProperty("line.separator");
                    fileWriter.write(info);
                }
    
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
       // 整体下载方案
    public void makeManyCsv(File file, EmpService empService,int count) { try { List<Emp> emps = empService.selectMany(count); FileWriter fileWriter = new FileWriter(file, true); int index = 0; for (Emp emp:emps) { index++; String info =""+index+","+ emp.asCsvLine()+ System.getProperty("line.separator"); fileWriter.write(info); } fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } // 当count过大时,分批下载 public void makePartialCsv(File file, EmpService empService,int count) { try { int PartialSize=10000; int times=count/PartialSize; for(int i=0;i<times;i++){ log.info("第"+i+"批次处理"); FileWriter fileWriter = new FileWriter(file, true); List<Emp> emps = empService.selectPartial(i*PartialSize, PartialSize); int index = i*PartialSize; for (Emp emp:emps) { index++; String info =""+index+","+ emp.asCsvLine()+ System.getProperty("line.separator"); fileWriter.write(info); } fileWriter.flush(); fileWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } }

    具体SQL实现:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
                        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.hy.csvdld.dao.EmpMapper">
    
        <select id="selectTenEmp" resultType="com.hy.csvdld.Entity.Emp">
            select id,name,age,cdate as ctime from emp order by id limit 10
        </select>
        
       <!-- 一次性取够 --> <select id="selectManyEmp" resultType="com.hy.csvdld.Entity.Emp"> select id,name,age,cdate as ctime from emp order by id limit #{count} </select>
    <!-- 分批多次取 --> <select id="selectPartialEmp" resultType="com.hy.csvdld.Entity.Emp"> select id,name,age,cdate as ctime from emp order by id limit #{start},#{size} </select> </mapper>

    程序的优化,归根结底是两条路:空间换时间 或是 时间换空间,本文的分批取做法,是时间换空间的路数。

    --END-- 2019年10月27日16:06:54

  • 相关阅读:
    融资担保公司
    典当公司
    保险代理、经纪公司互联网保险
    财产、人身、养老保险公司
    105家基金子公司
    LogStash Download
    cmd使用管理员权限运行,启动路径不是当前目录
    Module controller in JMeter
    Elasticsearch-->Get Started--> Exploring Your Data
    What are the differences between Flyweight and Object Pool patterns?
  • 原文地址:https://www.cnblogs.com/heyang78/p/11747855.html
Copyright © 2011-2022 走看看