In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use TK-MyBatis paging query". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Remember that tkMybatis queried a List collection that has been paged in a layer of Page encapsulation, that is, the queried list uses the type to determine that instanceof Page is true.
However, we do not understand that this is a collection with paging, so we do another layer to encapsulate the returned object type GoodsCategoryDTO. The code is as follows:
/ / Commodity collection List goodsCategorys = goodsLists.stream (). Map (x-> {GoodsCategoryDTO goodsDTO = new GoodsCategoryDTO (); goodsDTO.setGoodsId (x.getGoodsId ()); / / Price, name if (x.getPriceInfo (). GetPriceValue () = = null & & x.getPriceInfo (). GetPriceType () = null) {goodsDTO.setOriginalPrice (x.getPriceInfo (). GetOriginalPrice ()) } else {goodsDTO.setOriginalPrice (x.getPriceInfo (). GetOriginalPrice ()); goodsDTO.setPrice (x.getPriceInfo (). GetPriceValue () = = null? New BigDecimal ("): x.getPriceInfo (). GetPriceValue (); goodsDTO.setGoodsLable (x.getPriceInfo (). GetPriceName ());} goodsDTO.setTitle (x.getGoodsName ()); goodsDTO.setSubTitle (x.getGoodsSubname ()); goodsDTO.setImg (x.getGoodsImage ()); goodsDTO.setSkuId (x.getSkuId ()); return goodsDTO (Collectors.toList ()); return goodsCategorys
Literally, there is no problem in returning a collection of items that need to be paged in controller. When new a PageInfo passes the collection into the following code:
PageHelper.startPage (goodsParam.getPage (), goodsParam.getSize ()); / / set paging value / / return value List goodsCategoryDTO = goodsService.getGoodsCategoryInfo (goodsParam); PageInfo pageInfo = new PageInfo (goodsCategoryDTO); return ResultGenerator.genSuccessResult ("data returned successfully", pageInfo)
But never thought, in the process of creating a paging object PageInfo, goodsCategoryDTO this collection to determine the paging form, and then get the current page pageNum and pageSize and pageTotal according to page, but if goodsCategoryDTO uses instanceof to judge that Collection cannot be paged all the time.
Public PageInfo (List list, int navigatePages) {if (list instanceof Page) {Page page = (Page) list; this.pageNum = page.getPageNum (); this.pageSize = page.getPageSize (); this.pages = page.getPages (); this.list = page; this.size = page.size (); this.total = page.getTotal () / / since the result is > startRow, the actual need is + 1 if (this.size = = 0) {this.startRow = 0; this.endRow = 0;} else {this.startRow = page.getStartRow () + 1 / / calculate the actual endRow (special on the last page) this.endRow = this.startRow-1 + this.size;}} else if (list instanceof Collection) {this.pageNum = 1; this.pageSize = list.size (); this.pages = this.pageSize > 0? 1: 0; this.list = list This.size = list.size (); this.total = list.size (); this.startRow = 0; this.endRow = list.size () > 0? List.size ()-1: 0;} if (list instanceof Collection) {this.navigatePages = navigatePages; / / calculation navigation page calcNavigatepageNums (); / / calculation before and after page, first page, last page calcPage (); / / determine page boundary judgePageBoudary ();}}
It might be better to use the following solution to directly new a new Page object to get a new pageNum pageSize Total and assign it to it, then traverse the goodsList in the collection, then add it to the page collection twice and finally return the collection.
/ * query classified commodity information * @ param goodsParam * @ return * / public List getGoodsCategoryInfo (GoodsParam goodsParam) {/ / query the list List goodsLists = queryGoodsByCat (goodsParam) of the implemented and packaged Page; if (goodsLists instanceof Page) {Page goodsPage = (Page) goodsLists Return new Page () {{this.setPageNum (goodsPage.getPageNum ()); this.setPageSize (goodsPage.getPageSize ()); this.setTotal (goodsPage.getTotal ()); this.setPages (goodsPage.getPages ()) This.addAll (goodsPage.stream (). Map (goods-> new GoodsCategoryDTO () {{this.setGoodsId (goods.getGoodsId ()); this.setTitle (goods.getGoodsName ()); this.setSubTitle (goods.getGoodsSubname ()); this.setImg (goods.getGoodsImage () This.setSkuId (goods.getSkuId ()); if (goods.getPriceInfo (). GetPriceValue () = = null & & goods.getPriceInfo (). GetPriceType () = = null) {this.setOriginalPrice (goods.getPriceInfo (). GetOriginalPrice ());} else {this.setOriginalPrice (goods.getPriceInfo (). GetOriginalPrice ()) This.setPrice (goods.getPriceInfo (). GetPriceValue () = = null? New BigDecimal (""): goods.getPriceInfo (). GetPriceValue (); this.setGoodsLable (goods.getPriceInfo (). GetPriceName ());}}) .throw new IllegalStateException (Collectors.toList ());}};} else {throw new IllegalStateException ("goods list must be instance of Page") }} "how to use TK-MyBatis paging query" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.