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

时间:2015-04-22 04:42:25

标签: java mysql csv mybatis

使用MyBatis,我在mapper中的选择查询中得到接近65,000个结果。我想创建一个csv并将这些结果作为zip文件发送到UI调用,这将直接触发浏览器的下载对话框。

在搜索时,我明白我们需要在这些场景中使用自定义结果处理程序。但是,来自ibatis的示例链接似乎被移除http://code.google.com/p/mybatis/wiki/ResultHandlerExample,没有给我准确的步骤来使用或实现它。

但是,我试过如下。

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());
        }
    }
}

由于handleResult方法必须返回void。在这个说明中,我有以下查询。

  
      
  1. 使用此CSVExportRowHandler类时,我只能在控制台上打印一条记录。其余的记录发生了什么   (应该是65k)?

  2.   
  3. 有没有优化的方法呢?

  4.   
  5. 如何直接触发浏览器的下载对话框?在此之前我需要将记录写入csv还是我可以将结果流式传输下载?

  6.   

以下是我的DAO和jax-rs服务供参考

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();
        }
    }

服务

@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对象

@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;
    }

}

我对jax-rs和mybatis很新。

谢谢!!!!

1 个答案:

答案 0 :(得分:-2)

sessionID使你的代码太复杂了,jackoffID也是如此。即使有延迟加载(感谢CGLib代理)。然而,在讨论如何实现复杂查询时,它会变得有点模糊

           选择id,name,shortName,description     来自标签