在这里,我试图将两个结果作为一个回复返回,并且不确定哪个选项可能对它有好处。 在这里它以列表形式返回,
List<FixedDeposit> fdList = em.createQuery(sb.toString())
.setParameter("start", dateFrom)
.setParameter("end", dateTo)
.setFirstResult(start).setMaxResults(limit)
.getResultList();
所以我得到了
List<FDSearchResult> searchList = getSearchResult(fdList);
response.setData(searchList);
return response;
现在我又要回复另一个回复了。这是计算,
Double sum = 0.0;
sum = (Double) em.createQuery("SELECT SUM(deposit_amount)FROM fd_fixed_deposit where fd.status in ('ACT','REN','WDR') and fd.maturityDate between :start and :end ")
.setParameter("start", dateFrom)
.setParameter("end", dateTo).getSingleResult();
那么如何通过响应来返回结果呢?请帮忙
我将发布完整的方法,
public Response<List<FDSearchResult>> loadMaturedFDByRange(String fromDate, String toDate, final Integer start,
final Integer limit) {
final Response<List<FDSearchResult>> response = new Response<List<FDSearchResult>>();
Date dateFrom = DateUtils.parseDate("dd/MM/yyyy", fromDate, null);
Date dateTo = DateUtils.parseDate("dd/MM/yyyy", toDate, null);
Long count = 0L;
Double sum = 0.0;
try {
count = (Long) em.createQuery("SELECT COUNT(fd) FROM FixedDeposit fd where fd.status in ('ACT','REN','WDR') "
+ "and fd.maturityDate between :start and :end ")
.setParameter("start", dateFrom)
.setParameter("end", dateTo).getSingleResult();
} catch (NoResultException nre) {
count = 0L;
}
StringBuilder sb = new StringBuilder();
sb.append("SELECT fd FROM FixedDeposit fd where fd.status in ('ACT','REN','WDR') and fd.maturityDate between :start and :end ");
List<FixedDeposit> fdList = em.createQuery(sb.toString())
.setParameter("start", dateFrom)
.setParameter("end", dateTo)
.setFirstResult(start).setMaxResults(limit)
.getResultList();
sum = (Double) em.createQuery("SELECT SUM(deposit_amount)FROM fd_fixed_deposit where fd.status in ('ACT','REN','WDR') and fd.maturityDate between :start and :end ")
.setParameter("start", dateFrom)
.setParameter("end", dateTo).getSingleResult();
List<FDSearchResult> searchList = getSearchResult(fdList);
response.setData(searchList);
response.setPagination(new Response().new PaginationInfo(count));
return response;
}
答案 0 :(得分:3)
在这个例子中,我将创建一个Object Container来保存这两个对象并返回该对象。
public class MyContainer
{
List<FixedDeposit> fdList;
List<FDSearchResult> searchList;
public MyContainer()
{
}
}
我就是这样做的。
答案 1 :(得分:1)
您可以通过结果对象返回所有相关数据。 只需创建一个可以获得所有需要值的新类。
答案 2 :(得分:0)
两个选项:
HashMap中。类型转换对象分别为响应和Double。这不是一个好习惯
您可以创建一个可以封装不同对象类型的自定义类。 泛型是你需要的。
public class MultiReturn<T> {
public final String name;
public final T object;
public NamedObject(String name, T object) {
this.name = name;
this.object = object;
}
}