gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:45:43 25 4
gpt4 key购买 nike

使用 MyBatis,我从映射器中的一个选择查询中获得了接近 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. I am able to print to the console only one record upon using this CSVExportRowHandler class. What happened to the rest of the records (should have been 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 ?

下面是我的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 的新手。

谢谢!!!!

最佳答案

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

选择 ID、名称、短名称、描述 来自标签

关于java - 如何将 mybatis 选择查询的巨大结果集导出到 csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com