猿问

将 XML 响应拆分为页面

我想将 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;

}


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


我该如何实施?


泛舟湖上清波郎朗
浏览 114回答 1
1回答

潇湘沐

你怎么看这样的事情?对不起,这是未经测试的代码,但这样的事情应该可以工作。我创建了一个新的 PageInfo 类来存储分页信息。添加了一个查询以获取总行数并设置我的 page_info。然后限制查询结果的数量。最后将值设置为 ReconcilePaymentResponse。Class PageInfo {&nbsp; &nbsp; int current_page;&nbsp; &nbsp; int page_count;&nbsp; &nbsp; int per_page;&nbsp; &nbsp; int total_page;&nbsp; &nbsp; //constructor&nbsp; &nbsp; public PageInfo(int current_page, int page_count, int per_page) {&nbsp; &nbsp; &nbsp; &nbsp; //assign them&nbsp; &nbsp; }&nbsp; &nbsp; //getters&nbsp; &nbsp; //setters}SQL查询:public List<PaymentTransactions> transactionsByDate(LocalDateTime start_date, LocalDateTime end_date, Merchants merchant, Terminals terminal,&nbsp; &nbsp; PageInfo pageInfo) throws Exception {&nbsp; &nbsp; //figure out number of total rows&nbsp; &nbsp; String count_hql = "select count(*) from " + PaymentTransactions.class.getName() + " e where e.created_at >= ? and e.created_at <= ?";&nbsp; &nbsp; Query count_query = entityManager.createQuery(count_hql);&nbsp; &nbsp; int count = countQuery.uniqueResult();&nbsp; &nbsp; //figure out total pages&nbsp; &nbsp; int total_page = (int)Math.ceil(count/(double)pageInfo.getPerPage());&nbsp; &nbsp; pageInfo.setTotal_Page(total_page);&nbsp; &nbsp; String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.created_at >= ? and e.created_at <= ?";&nbsp; &nbsp; Query query = entityManager.createQuery(hql)&nbsp; &nbsp; &nbsp; &nbsp; //set starting point&nbsp; &nbsp; &nbsp; &nbsp; .setFirstResult((pageInfo.getCurrentPage()-1) * pageInfo.getPerPage)&nbsp; &nbsp; &nbsp; &nbsp; //set max rows to return&nbsp; &nbsp; &nbsp; &nbsp; .setMaxResults(pageInfo.getPerPage)&nbsp; &nbsp; &nbsp; &nbsp; .setParameter(0, start_date).setParameter(1, end_date);&nbsp; &nbsp; List<PaymentTransactions> paymentTransactions = (List<PaymentTransactions>) query.getResultList();&nbsp; &nbsp; return paymentTransactions;}返回 XML:&nbsp; &nbsp; &nbsp; &nbsp; //initialize PageInfo with desired values&nbsp; &nbsp; &nbsp; &nbsp; PageInfo page_info = new PageInfo(1,10,4);&nbsp; &nbsp; &nbsp; &nbsp; List<PaymentTransactions> paymentTransactions = transactionsService&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .transactionsByDate(reconcile.getStart_date(), reconcile.getEnd_date(), merchant, terminal, page_info);&nbsp; // pass in page_info&nbsp; &nbsp; &nbsp; &nbsp; ReconcilePaymentResponses pr = new ReconcilePaymentResponses();&nbsp; &nbsp; &nbsp; &nbsp; pr.setPage(page_info.getCurrentPage());&nbsp; &nbsp; &nbsp; &nbsp; pr.setPages_count(page_info.getPageCount());&nbsp; &nbsp; &nbsp; &nbsp; pr.setPer_page(page_info.getPerPage());&nbsp; &nbsp; &nbsp; &nbsp; pr.setTotal_count(String.valueOf(paymentTransactions.size()));&nbsp; &nbsp; &nbsp; &nbsp; for (int e = 0; e < paymentTransactions.size(); e++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PaymentTransactions pt = paymentTransactions.get(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReconcilePaymentResponse obj = new ReconcilePaymentResponse();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.setTransaction_type(pt.getType());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pr.getPaymentResponse().add(obj);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return pr;
随时随地看视频慕课网APP

相关分类

Java
我要回答