多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 一、概述 列表的数据,一般依据一个表/视图的数据查询出来,得到列表的结果;但是,有时候会出现,单表/视图的数据还不够,业务上需要列表中的每条数据的更多关联信息; 例如:列表是一个人的数据记录列表,但也没上,一次性还需要每个人的每一段工作经历;那么,单一的人员记录列表列表就不能满足需要,还需要每个人的每一段工作经历(列表); ## 二、解决方案 举例说明: ### **使用** ``` @RayServiceMethod public ResponseObject list(String keyWord, int curPage, long boardType) throws Exception { Specification<DouyinBoardCommon> spec = new Specification<DouyinBoardCommon>() { @Override public Predicate toPredicate(Root<DouyinBoardCommon> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { List<Predicate> predicates = new ArrayList<>(); predicates.add(criteriaBuilder.equal(root.get("boardType"), boardType)); return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])); } }; model.setPageobject(fetchRowExtraObjects(PageProcessor.reasonablePage(douyinBoardCommonDao, spec, curPage, RayGlobalConfig.getGlobalProperties().getRayDefault().getPageSize(), "curRank", OrderDirection.ASC))); return responseObj; } ``` ### **定义复合对象** ``` static class DouyinBoardCommonWithVideos { private DouyinBoardCommon board; private List<DouyinBoardCommonVideo> videoList; public DouyinBoardCommon getBoard() { return board; } public void setBoard(DouyinBoardCommon board) { this.board = board; } public List<DouyinBoardCommonVideo> getVideoList() { return videoList; } public void setVideoList(List<DouyinBoardCommonVideo> videoList) { this.videoList = videoList; } } ``` ### **处理分页的数据** ``` private Page fetchRowExtraObjects(Page pageObject) { List<DouyinBoardCommon> originalListInPage = pageObject.getContent(); List mixedObjectWithExtraList = new ArrayList(); if (!ListUtil.isEmpty(originalListInPage)) { for (DouyinBoardCommon rowObject : originalListInPage) { DouyinBoardCommonWithVideos mixedObjectWithExtra = new DouyinBoardCommonWithVideos(); mixedObjectWithExtra.setBoard(rowObject); mixedObjectWithExtra.setVideoList(douyinBoardCommonVideoDao.getDouyinBoardCommonVideoOfSpecBoard(rowObject.getBoardId())); mixedObjectWithExtraList.add(mixedObjectWithExtra); } ByteCodeUtil.forcedModifyObjecctValueWhenFieldInHierarchy(pageObject, "content", mixedObjectWithExtraList); } return pageObject; } ``` ### **处理合并对象的版本** ``` private Page fetchRowExtraObjects(Page pageObject) throws Exception { List<SysDomain> originalListInPage = pageObject.getContent(); List mixedObjectWithExtraList = new ArrayList(); if (!ListUtil.isEmpty(originalListInPage)) { for (SysDomain rowObject : originalListInPage) { mixedObjectWithExtraList.add(ObjectCombiner.mergeObjects(rowObject, sysCorporationDao.findByCorporationEntityTypeAndCorporationEntityId(CorporationEntityType.DOMAIN, rowObject.getDomainId()))); } ByteCodeUtil.forcedModifyObjecctValueWhenFieldInHierarchy(pageObject, "content", mixedObjectWithExtraList); } return pageObject; } ``` >[danger] > 1、这里需要注意,前端做分页的时候,必须注意到,这个 字段是合成的,不能直接拿来传到后台做分页字段; > 2、如果是非分页的列表,也可以使用本方案,做法一样;