将XML响应分成页面

时间:2018-10-07 12:43:33

标签: java sql hibernate jpa jaxb

我想将XML响应分成多个页面,因为我有太多的XML项目无法发送回去。我尝试过:

XML请求:

<?xml version="1.0" encoding="UTF-8"?>
<reconcile>
  <start_date>2018-04-08T11:02:44</start_date>
  <end_date>2019-10-08T11:02:44</end_date>
  <page>1</page>
</reconcile>

JAXB:

@XmlRootElement(name = "reconcile")
@XmlAccessorType(XmlAccessType.FIELD)
public class Reconcile {

    @XmlElement(name = "start_date")
    @XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
    private LocalDateTime start_date;
    @XmlElement(name = "end_date")
    @XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
    private LocalDateTime end_date;
    @XmlElement(name = "page")
    private String page;
    ...../// getters and setters
}

SQL查询:

public List<PaymentTransactions> transactionsByDate(LocalDateTime start_date, LocalDateTime end_date, Merchants merchant, Terminals terminal) throws Exception {

        String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.created_at >= ? and e.created_at <= ?";
        Query query = entityManager.createQuery(hql).setParameter(0, start_date).setParameter(1, end_date);
        List<PaymentTransactions> paymentTransactions = (List<PaymentTransactions>) query.getResultList();
        return paymentTransactions;
}

返回XML:

 List<PaymentTransactions> paymentTransactions = transactionsService
                    .transactionsByDate(reconcile.getStart_date(), reconcile.getEnd_date(), merchant, terminal);

            ReconcilePaymentResponses pr = new ReconcilePaymentResponses();
            pr.setPage("1");
            pr.setPages_count("10");
            pr.setPer_page("4");
            pr.setTotal_count(String.valueOf(paymentTransactions.size()));

            for (int e = 0; e < paymentTransactions.size(); e++) {
                PaymentTransactions pt = paymentTransactions.get(e);

                ReconcilePaymentResponse obj = new ReconcilePaymentResponse();
                obj.setTransaction_type(pt.getType());
                pr.getPaymentResponse().add(obj);
            }

            return pr;

XML响应:

<?xml version='1.0' encoding='UTF-8'?>
<payment_responses page="1" per_page="4" total_count="5" pages_count="10">
    <payment_response>
        <transaction_type>Type</transaction_type>
    </payment_response>
    <payment_response>
        <transaction_type>Type</transaction_type>
    </payment_response>
    <payment_response>
        <transaction_type>Type</transaction_type>
    </payment_response>
    .........
</payment_responses>

我想以某种方式将<payment_response>....</payment_response>分成页面以减少内存开销。例如,当我发送1时,我想返回前10个。

我该如何实现?

1 个答案:

答案 0 :(得分:1)

您如何看待这样的事情?很抱歉,这是未经测试的代码,但是这样的事情应该可以工作。

我创建了一个新的PageInfo类来存储页面信息。添加了一个查询以获取总行数并设置我的page_info。然后限制查询结果的数量。最后,将值设置为ReconcilePaymentResponse。

Class PageInfo {
    int current_page;
    int page_count;
    int per_page;
    int total_page;

    //constructor
    public PageInfo(int current_page, int page_count, int per_page) {
        //assign them
    }
    //getters
    //setters
}

SQL查询:

public List<PaymentTransactions> transactionsByDate(LocalDateTime start_date, LocalDateTime end_date, Merchants merchant, Terminals terminal,
    PageInfo pageInfo) throws Exception {

    //figure out number of total rows
    String count_hql = "select count(*) from " + PaymentTransactions.class.getName() + " e where e.created_at >= ? and e.created_at <= ?";
    Query count_query = entityManager.createQuery(count_hql);
    int count = countQuery.uniqueResult();

    //figure out total pages
    int total_page = (int)Math.ceil(count/(double)pageInfo.getPerPage());
    pageInfo.setTotal_Page(total_page);

    String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.created_at >= ? and e.created_at <= ?";
    Query query = entityManager.createQuery(hql)
        //set starting point
        .setFirstResult((pageInfo.getCurrentPage()-1) * pageInfo.getPerPage)
        //set max rows to return
        .setMaxResults(pageInfo.getPerPage)
        .setParameter(0, start_date).setParameter(1, end_date);
    List<PaymentTransactions> paymentTransactions = (List<PaymentTransactions>) query.getResultList();
    return paymentTransactions;
}

返回XML:

        //initialize PageInfo with desired values
        PageInfo page_info = new PageInfo(1,10,4);
        List<PaymentTransactions> paymentTransactions = transactionsService
            .transactionsByDate(reconcile.getStart_date(), reconcile.getEnd_date(), merchant, terminal, page_info);  // pass in page_info

        ReconcilePaymentResponses pr = new ReconcilePaymentResponses();
        pr.setPage(page_info.getCurrentPage());
        pr.setPages_count(page_info.getPageCount());
        pr.setPer_page(page_info.getPerPage());
        pr.setTotal_count(String.valueOf(paymentTransactions.size()));

        for (int e = 0; e < paymentTransactions.size(); e++) {
            PaymentTransactions pt = paymentTransactions.get(e);

            ReconcilePaymentResponse obj = new ReconcilePaymentResponse();
            obj.setTransaction_type(pt.getType());
            pr.getPaymentResponse().add(obj);
        }

        return pr;