把两个list合并为一个新的list

it2022-05-05  110

由于功能的需求是需要把两张表的数据结合起来,然后再同一个表格展示 。所以屡一下思路就是先声明一个同类型的空的list做最后输出给表格的list,再声明两个list集合,方别对应一张表,然后把数据查出来,遍历list,把数据一条一条的添加到需要展示的list里面。

利用as Name来把他们的字段名称统一。

//第一张表的sql语句 private String TJXM = "select a.BodyCheckItemName as Name,a.BodyCheckItemNum as Code, CureItemsPrice as JG,OrTreatment "+ " from SYS_BodyCheckItem a, SYS_ItemCommon b where a.ItemCommonID = b.ItemCommonID and BodyCheckItemID = ?"+ " order by a.BodyCheckItemID desc offset (?-1)*? row fetch next ? row only"; //第二表的sql语句 private String TJTC = "Select BCheckComboName as Name,ComboNum as Code,CashSum as JG from SYS_BodyCheckCombo where BCheckComboID = ?"+ " order by BCheckComboID desc offset (?-1)*? row fetch next ? row only"; private String SQL2 = "select * from PW_Recipe a ,PW_AssistItem b where a.RecipeID = b.RecipeID and a.RecipeID = ?"; @Override public List<BodyCheckItemCommSele> TabChargeXM(String RecipeID, int currentPage, int pageSize) { if(RecipeID == null || "".equals(RecipeID)){ return null; } //创建新的list List<BodyCheckItemCommSele> list = new ArrayList<BodyCheckItemCommSele>(); Object[] obj = {RecipeID}; List<ReipeAnAssistItem> list2 = DBUtil.selectAll(SQL2, obj, ReipeAnAssistItem.class); if(list2.size() > 0){ for (int i = 0; i < list2.size(); i++) { int ItemID = list2.get(i).getItemID(); if(list2.get(i).getItemTypeID() == 8043){ Object[] obj2 = {ItemID,currentPage,pageSize,pageSize}; List<BodyCheckItemCommSele> list3 = DBUtil.selectAll(TJXM,obj2, BodyCheckItemCommSele.class); if(list3.size() > 0){ for (int j = 0; j < list3.size(); j++) { list3.get(j).setRecipeDetailType("体检项目"); list3.get(j).setUseAmount("1"); list3.get(j).setPieceUnit("次"); list3.get(j).setWhetherHealthCare(list3.get(j).getOrTreatment()); //把数据添加到新的list list.add(list3.get(j)); } } } if(list2.get(i).getItemTypeID() == 8046){ Object[] obj2 = {ItemID,currentPage,pageSize,pageSize}; List<BodyCheckItemCommSele> list4 = DBUtil.selectAll(TJTC,obj2, BodyCheckItemCommSele.class); if(list4.size() > 0){ for (int j = 0; j < list4.size(); j++) { list4.get(j).setRecipeDetailType("体检套餐"); list4.get(j).setUseAmount("1"); list4.get(j).setPieceUnit("次"); list4.get(j).setWhetherHealthCare("否"); //把数据添加到新的list list.add(list4.get(j)); } } } } } //返回新的list return list; }


最新回复(0)