为了账号安全,请及时绑定邮箱和手机立即绑定

将 XML 响应拆分为页面

将 XML 响应拆分为页面

泛舟湖上清波郎朗 2021-11-03 15:06:35
我想将 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 个。我该如何实施?
查看完整描述

1 回答

?
潇湘沐

TA贡献1816条经验 获得超6个赞

你怎么看这样的事情?对不起,这是未经测试的代码,但这样的事情应该可以工作。


我创建了一个新的 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;


查看完整回答
反对 回复 2021-11-03
  • 1 回答
  • 0 关注
  • 113 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信