gpt4 book ai didi

java - 按元素对列表项进行分组以在 jsp 收据中使用

转载 作者:行者123 更新时间:2023-11-30 04:41:40 27 4
gpt4 key购买 nike

下面有一些代码,用于查询数据库并将数据添加到列表中,

Query q= session.createQuery("select tally_receipt_prefix, tally_receipt_no,           tally_head, tally_amount from Tally_table where tally_system_date='"+fmtd_date+"' and tally_dbcr_indicator='DB' and tally_mode='Ca' order by  tally_head,tally_receipt_prefix,tally_receipt_no");

payincash = new ArrayList();

for(Iterator it=q.iterate(); it.hasNext(); )
{
Object[] row= (Object[]) it.next();
payincash.add((String)row[0]);
payincash.add((String)row[1]);
payincash.add((String)row[2]);
payincash.add((String)row[3]);

}

System.out.println("cash list in dao: "+payincash);

返回的列表类似于 [prefix1, no1, head1, amt1, prefix2, no2, head2, amt2,]。我正在尝试在 jsp 中按以下行制作收据

头1

前缀1/no1 amt1

前缀2/no2 amt 2

<小时/>

头3

前缀3/no3 amt3

看起来我想按收据 - jsp 文件中的头列对所有记录进行分组。我该怎么办?任何帮助都非常感激。请原谅我的英语。

编辑:这是我尝试过的,

查询q= session.createQuery("从Tally_table中选择tally_receipt_prefix、tally_receipt_no、tally_head、tally_amount,其中tally_system_date='"+fmtd_date+"'和tally_dbcr_indicator='DB'和tally_mode='Ca'按tally_head、tally_receipt_prefix、tally_receipt_no排序"); System.out.println("查询"+q);

    List heads=new ArrayList();

for(Iterator it=q.iterate(); it.hasNext(); )
{
Object[] row= (Object[]) it.next();

payincash1=new LinkedHashMap<String, List>();

heads.add((String)row[2]);

List tails = null;
tails=new ArrayList();
tails.add((String)row[0]);
tails.add((String)row[1]);
tails.add((String)row[3]);

System.out.println("heads in dao from iter 1: "+heads);
System.out.println("tails in dao from iter1 on: "+tails);

if(heads.contains((String)row[2])) // for head in temp list
{
System.out.println("in first if");
if(payincash1.containsKey((String)row[2]))
{
System.out.println("map if repeat: "+payincash1);
payincash1.put((String)row[2],tails);
}

}
else
{

System.out.println("map if not repeat: "+payincash1);
payincash1.put((String)row[2], tails);

}

}

最佳答案

收据的头栏存储在哪里?在哪一栏?
以我的愚见,它也应该存储在数据库中。
假设头信息保存在数据库的“head”列中,因此您应该通过添加以下内容来更改查询:

order by head

最后。
之后,您应该迭代结果,并且可能将信息保存在如下所示的数据结构中:

Map<String,List<ReceiptInformation> map = new HashMap<>(); //using JDK7 syntax here

映射中的键应该是每次迭代中“head”的值。映射中的值应该是保存 ReceiptInfo 对象的 ArrayList(或任何其他实现 List 的类)。
ReceiptInfoObject 保存每条记录的所有其余值。
然后,您可以迭代 map.keySet() 集合,并为每个键打印头部,然后使用内部循环打印收据。

根据提出问题的用户的请求进行编辑:
为了添加新条目(即新的 RecepitInformation 对象到 map ),应该执行:

List<RecepitInformation> listForHead = map.get(headValue);
if (listForHead == null) {
listForHead = new ArrayList<ReceiptInformation>();
map.put(headValue,listForHead);
}
listForHead.add(recepitInformation);

像往常一样,我没有编译它,但我认为它应该可以工作

关于java - 按元素对列表项进行分组以在 jsp 收据中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12147997/

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