Note: tkMybatis query to find a List collection. This collection has been packaged with a layer of page page, that is, the query list used to determine the instanceof Page is true
However, I didn't understand that this was a paged collection, and I encapsulated the query result set in another layer. The object type that needs to be returned is GoodsCategoryDTO, the code is as follows:
// Product collection List<GoodsCategoryDTO> goodsCategorys = ().map(x->{ GoodsCategoryDTO goodsDTO = new GoodsCategoryDTO(); (()); // Price, name if(().getPriceValue() == null && ().getPriceType() == null){ (().getOriginalPrice()); }else { (().getOriginalPrice()); (().getPriceValue() == null ? new BigDecimal(""): ().getPriceValue()); (().getPriceName()); } (()); (()); (()); (()); return goodsDTO; }).collect(()); return goodsCategorys;
literally understand, no problem. Return a collection of products that need to be exported. When doing pagination in the controller, new PageInfo pass the collection into the code as follows:
((), ()); // Set the page value // Return value List<GoodsCategoryDTO> goodsCategoryDTO = (goodsParam); PageInfo<GoodsCategoryDTO> pageInfo = new PageInfo<>(goodsCategoryDTO); return ("Return to data successfully", pageInfo);
But I never expected that in the process of creating the page object PageInfo, the collection of goodsCategoryDTO is used to judge the paging form, and then the current page pageNum, pageSize and pageTotal is obtained according to the page. However, if goodsCategoryDTO uses instanceof to determine that the collection cannot be paginated.
public PageInfo(List<T> list, int navigatePages) { if (list instanceof Page) { Page page = (Page) list; = (); = (); = (); = page; = (); = (); //Because the result is >startRow, the actual need is +1 if ( == 0) { = 0; = 0; } else { = () + 1; // Calculate the actual endRow (special at the last page) = - 1 + ; } } else if (list instanceof Collection) { = 1; = (); = > 0 ? 1 : 0; = list; = (); = (); = 0; = () > 0 ? () - 1 : 0; } if (list instanceof Collection) { = navigatePages; // Calculate navigation page calcNavigatepageNums(); // Calculate the previous and last pages, the first page, the last page calcPage(); //Judge page boundaries judgePageBoudary(); } }
I thought about it and used the following solution to make it better. Directly new a new Page object Get the new pageNum pageSize Total and assign it to the object, then iterate through the goodsList in the collection and then do secondary encapsulation Add to the page collection and finally return the collection.
/** * Query classified product information * @param goodsParam * @return */ public List<GoodsCategoryDTO> getGoodsCategoryInfo (GoodsParam goodsParam){ // Query the list of the page that has been implemented and encapsulated List<GoodsDTO> goodsLists = queryGoodsByCat(goodsParam); if (goodsLists instanceof Page<?>) { Page<GoodsDTO> goodsPage = (Page<GoodsDTO>) goodsLists; return new Page<GoodsCategoryDTO>() {{ (()); (()); (()); (()); (().map(goods -> new GoodsCategoryDTO() {{ (()); (()); (()); (()); (()); if(().getPriceValue() == null && ().getPriceType() == null){ (().getOriginalPrice()); }else { (().getOriginalPrice()); (().getPriceValue() == null ? new BigDecimal(""): ().getPriceValue()); (().getPriceName()); } }}).collect(())); }}; } else { throw new IllegalStateException("goods list must be instance of Page"); } }
This is the article about the specific use of TK-MyBatis pagination query. For more related contents of TK-MyBatis pagination query, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!