简体   繁体   English

如何将mybatis选择查询的巨大结果集导出到csv?

[英]How to export huge result set of a mybatis select query to csv?

Using MyBatis, I am getting close to 65,000 results from a select query within a mapper. 使用MyBatis,我从映射器中的选择查询中获得了近65,000个结果。 I want to create a csv and send these results as a zip file to the UI call which will trigger the download dialog directly of the browser. 我想创建一个csv并将这些结果作为zip文件发送到UI调用,这将直接触发浏览器的下载对话框。

Upon searching, I understood that we need to use custom result handler in these scenarios. 搜索后,我了解到在这些情况下我们需要使用自定义结果处理程序。 But the link to the example from ibatis seems to be removed http://code.google.com/p/mybatis/wiki/ResultHandlerExample leaving no accurate steps for me to use or implement it. 但是ibatis中该示例的链接似乎已被删除http://code.google.com/p/mybatis/wiki/ResultHandlerExample ,因此我没有使用或实现它的准确步骤。

However, I have tried as below. 但是,我尝试如下。

public class CSVExportRowHandler implements ResultHandler {
    @SuppressWarnings("unchecked")
    @Override
    public void handleResult(ResultContext context) {
        SRDContainer srdContainer = (SRDContainer) context.getResultObject();
        System.out.println("Container: " + srdContainer.toString() + ", " + context.getResultCount() +", "+ context.isStopped());
        for (StudentResultDetails srd : srdContainer.getStudentResultDetails()){
            System.out.println("result: " + srd.getStudentName());
        }
    }
}

As the handleResult method must return void. 由于handleResult方法必须返回void。 On this note, I have the below queries. 在此注意,我有以下查询。

  1. I am able to print to the console only one record upon using this CSVExportRowHandler class. 使用此CSVExportRowHandler类时,我只能将一条记录打印到控制台。 What happened to the rest of the records (should have been 65k) ? 其余记录(应该是65k)怎么了?

  2. Is there any optimized way of doing this ? 有没有优化的方式来做到这一点?

  3. How to trigger the download dialog of browser directly ? 如何直接触发浏览器的下载对话框? before that do I need to write the records to csv or I can stream the results to download ? 在此之前,我需要将记录写入csv还是可以流处理结果进行下载?

Here are my DAO and the jax-rs service for reference 这是我的DAO和jax-rs服务以供参考

DAO.java DAO.java

public void exportSRD(HashMap<String, Object> params) {         
        SqlSession sqlSession = SessionFactory.getSession().openSession();
        try {
            CSVExportRowHandler handler = new CSVExportRowHandler();
            sqlSession.select("getAssignmentSRDSession", params, handler);

        } finally {
            sqlSession.close();
        }
    }

Service: 服务:

@Path("/{reportType}/studentresultdetails/{function}/{sessionId}")
@GET
public void exportOrPrintUtil(@PathParam("reportType") String reportType, @PathParam("function") String function, @QueryParam("pageNum") Integer pageNum,
                @QueryParam("pageSize") Integer pageSize, @QueryParam("sortOn") String sortOn, @QueryParam("sortOrder") String sortOrder, 
                @QueryParam("filterByName") String filterByName, @PathParam("sessionId") Integer sessionId) throws IOException {
    HashMap<String, Object> params = new HashMap<String, Object>();
            Object enableSort;
            Object enablePrintOrExportFlag;
            params.put("sessionId", sessionId);
            params.put("enableSort", false);
            params.put("enablePrintOrExportFlag", true);
            params.put("filterByName", filterByName);

            dao.exportSRD(params);

            *********** WHAT TO BE RETURNED AS THE ABOVE dao.exportSRD(params) RETURNS VOID. HOW TO TRIGGER DOWNLOAD DIALOG FROM HERE. *****************

        }

SRDContainer.java object SRDContainer.java对象

@XmlRootElement
public class SRDContainer implements Comparable<SRDContainer> {

    private List<StudentResultDetails> studentResultDetails;

    public SRDContainer() {}

    public SRDContainer(
            List<StudentResultDetails> studentResultDetails) {
        super();
        this.studentResultDetails = studentResultDetails;
    }

    public List<StudentResultDetails> getStudentResultDetails() {
        return studentResultDetails;
    }

    public void setStudentResultDetails(
            List<StudentResultDetails> studentResultDetails) {
        this.studentResultDetails = studentResultDetails;
    }

    @Override
    public int compareTo(SRDContainer o) {
        return 0;
    }

    @Override
    public String toString() {
        return "SRDContainer [studentResultDetails="
                + studentResultDetails + "]";
    }

    public List<String[]> toStringArrayList() {
        String[] array = new String[studentResultDetails.size()];
        List<String[]> stringArrayListOut = new ArrayList<String[]>();
        int index = 0;
        for (StudentResultDetails value : this.studentResultDetails) {
          array[index] = value.toFlatCSVString();
          index++;
        }
        stringArrayListOut.add(array);
        return stringArrayListOut;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime
                * result
                + ((studentResultDetails == null) ? 0 : studentResultDetails
                        .hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SRDContainer other = (SRDContainer) obj;
        if (studentResultDetails == null) {
            if (other.studentResultDetails != null)
                return false;
        } else if (!studentResultDetails.equals(other.studentResultDetails))
            return false;
        return true;
    }

}

I am pretty new to jax-rs and mybatis. 我对jax-rs和mybatis相当陌生。

Thanks !!!! 谢谢 !!!!

sessionID make your code too complicated , jackoffID as well. sessionID会使您的代码过于复杂,jackoffID也是如此。 even with deferred loading (thanks to CGLib proxies). 即使是延迟加载(也要感谢CGLib代理)。 It gets just a little fuzzy, however, when discussing how to implement complex queries 但是,在讨论如何实现复杂查询时,它有点模糊

select id, name, shortName, description from tag 从标签中选择ID,名称,shortName,描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM