前文里谈过一次性从数据库取一个大结果集有可能导致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

【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)的更多相关文章

  1. 利用MySQL数据库如何解决大数据量存储问题?

    提问:如何设计或优化千万级别的大表?此外无其他信息,个人觉得这个话题有点范,就只好简单说下该如何做,对于一个存储设计,必须考虑业务特点,收集的信息如下:1.数据的容量:1-3年内会大概多少条数据,每条 ...

  2. PHP7语法知识(四):目录文件操作、Cookie与Session、MySQL数据库的使用、Redis数据库、PHP处理XML与JSON

    目录文件操作 一.目录 1.判断文件类型: 2.创建和删除目录: 3.打开读取和关闭目录 4.获得路径中目录部分 5.目录磁盘空间 二.文件操作 1.打开文件: 2.读取文件: 3.获得文件属性: 4 ...

  3. 大数据入门到精通13--为后续和MySQL数据库准备

    We will be using the sakila database extensively inside the rest of the course and it would be great ...

  4. 利用mysql数据库日志文件获得webshell

    查看配置 show variables like '%general%'; 开启日志功能 set GLOBAL general_log='ON'; 设置日志存储路径 SET GLOBAL genera ...

  5. 大数据的存储——HBase、HIVE、MYSQL数据库学习笔记

    HBase 1.hbase为查询而生,它通过组织机器的内存,提供一个超大的内存hash表,它需要组织自己的数据结构,表在hbase中是物理表,而不是逻辑表,搜索引擎用它来存储索引,以满足实时查询的需求 ...

  6. JavaWeb 文件上传下载

    1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端,从服务器端下载文件到本地的过程.例如目前网站需要上传头像.上传下载图片或网盘等功能都是利用文件上传下 ...

  7. 转载:JavaWeb 文件上传下载

    转自:https://www.cnblogs.com/aaron911/p/7797877.html 1. 文件上传下载概述 1.1. 什么是文件上传下载 所谓文件上传下载就是将本地文件上传到服务器端 ...

  8. MySQL数据库如何解决大数据量存储问题

    利用MySQL数据库如何解决大数据量存储问题? 各位高手您们好,我最近接手公司里一个比较棘手的问题,关于如何利用MySQL存储大数据量的问题,主要是数据库中的两张历史数据表,一张模拟量历史数据和一张开 ...

  9. MySQL数据库中.SQL文件的导出方式

    转自:http://tech.watchstor.com/management-117401.htm 在MySQL数据库中导入SQL文件是件很麻烦的事情,但是这是一项大家非常值得学习的技术,本文就从最 ...

随机推荐

  1. drunk_admin_hacking_challenge靶机之旅?

    注:  只是记录本人玩的时候发现的新奇点  如果你也想玩且看了这篇文章还是不会,请联系gg 靶机下载地址 https://www.vulnhub.com/entry/drunk-admin-web-h ...

  2. Android写入文件电脑看不到

    原因是因为没有刷新,写入文件后执行以下代码即可. 根目录路径: File newfile = new File(Environment.getExternalStorageDirectory() + ...

  3. tomcat压缩配置

    问题描述:HPS打开登录页面(也就是用户输入用户名和密码的页面),要加载数据和程序,大概2M大小,在网络不好的情况下,要10几秒甚至几十秒,公司内网测试需要:3秒多 解决方法: 1. 打开登录页面,用 ...

  4. 最最常用的RAID

    若转载请于明显处标明出处:http://www.cnblogs.com/kelamoyujuzhen/p/8980696.html RAID stands for Redundant Array of ...

  5. linux 基础7-正则表达式

    1. 基础正规表示法 1.1 以grep获取字符串: 在万用字符*是0-无限个字符,?是一个字符:在正则表达式中是0-无限个字符前一个相同字符..一个前一个相同字符 grep '^[a-z]' gre ...

  6. 315 · Istio1.1 功能预告,真的假不了

    Istio 1.0版本发布到现在,已经过去8个月.Istio1.1的候选版本也到了rc5,预计近期会正式发布1.1.此版本包含了许多错误修复,在流量管理,安全,策略和遥测,多集群等领域添加了新的功能. ...

  7. idou老师教你学istio 21:基于角色的访问控制

    istio的授权功能,也称为基于角色的访问控制(RBAC),它为istio服务网格中的服务提供命名空间级别.服务级别和方法级别的访问控制.基于角色的访问控制具有简单易用.灵活和高性能等特性.本文介绍如 ...

  8. 《构建之法》——Alpha2项目的测试

    这个作业属于哪个课程 课程的链接 这个作业要求在哪里 作业要求的链接 团队名称 Runningman 这个作业的目标 测试其他组的项目,互相借鉴 作业正文 作业正文 测试人姓名 陈嘉莹 测试人学号 2 ...

  9. php中unserialize 返回false的解决方法

    serialize 序列化 unserialize 反序列化 $content = 'a:5:{s:4:"img1";s:5:"35568";s:4:" ...

  10. 关于tcp send的再次思考

    最近在用socket时,再次思考了一下如何确保对方收到消息的问题 下面是一些不错的回答 https://www.zhihu.com/question/25016042/answer/73785738 ...