In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to achieve paging in Java project development" is not understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve paging in Java project development" article.
Preface
Paging function is often used in Java project development, and now SpringBoot is widely used for rapid development, while the data layer mainly integrates SpringDataJPA and MyBatis frameworks, both of which provide corresponding paging tools, and the way to use them is also very simple, but I also use the third more convenient and flexible paging method in my work to share with you here.
Use
It is mainly divided into SpringDataJPA paging, MyBatis paging and Hutools tool paging.
1. SpringDataJPA paging
1), introduce dependency
Org.springframework.boot spring-boot-starter-data-jpa
2), write paging service in Service
SpringDataJPA paging is to define Pageable object to handle paging, in which PageRequest defines paging parameters, Page object takes over the query results for paging packaging, the packaged result pageResult can get the total number of records, total pages, paging list and other data results.
/ * query all watch lists according to doctorId [paging] * * @ param doctorId Doctor id * @ return result set * / public Map findAllListByDoctorId (Long doctorId, Integer pageIndex, Integer pageSize) {Pageable pageable = PageRequest.of (pageIndex-1, pageSize); / / paging Page pageResult = followRepository.findByDoctorIdOrderByCreatedAtDesc (doctorId, pageable); List dtoList = followMapper.toDto (pageResult.getContent ()) If (! CollectionUtils.isEmpty (dtoList)) {/ / deals with business logic. } / / encapsulate the paging result Map map = new HashMap (); map.put ("pageIndex", pageIndex.toString ()); / / current page map.put ("pageSize", pageSize.toString ()); / / number of entries per page map.put ("total", Long.toString (pageResult.getTotalElements (); / / Total number of records map.put ("pages", Integer.toString (pageResult.getTotalPages () / / Total number of pages map.put ("list", dtoList); / / data list return map;}
3), dealing with paging in Repository
Here you inherit the JpaRepository object, pass in the defined pageable object in service, and return the result wrapped by Page.
Import com.patient.domain.Follow;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.jpa.repository.JpaRepository; @ Repositorypublic interface FollowRepository extends JpaRepository {Page findByDoctorIdOrderByCreatedAtDesc (Long doctorId, Pageable pageable);} 2, MyBatis paging
1) introduce PageHelper dependency
Com.github.pagehelper pagehelper-spring-boot-starter
2) use PageHelper to realize paging
/ * query the list of promoters and page it. * @ return encapsulated paging result object * / public PageResult findPromotePersonList (String hospitalCode,PromotePersonReq promotePersonReq) {Integer pageIndex = promotePersonReq.getPageIndex (); Integer pageSize = promotePersonReq.getPageSize (); PageHelper.startPage (pageIndex, pageSize); / / the size of each page is pageSize. Query the result of page List list = promotePersonMapper.selectAll (); PageInfo pageInfo = new PageInfo (list); PageHelper.clearPage (); / / return the paging result PageResult pageResult = new PageResult () PageResult.setPageIndex (pageIndex); pageResult.setPageSize (pageSize); pageResult.setPages (pageInfo.getPages ()); pageResult.setTotal ((int) pageInfo.getTotal ()); pageResult.setList (list); return pageResult;} 3, Hutools tool paging
1), introduce dependency
Here you can introduce some tool classes of hutools separately. Refer to the official website documentation. I usually use this tool to write projects, so I directly introduce all of them.
Cn.hutool hutool-all 5.1.2
2), paging implementation
Generally use two tool classes, one is PageUtil.totalPage (total records, records per page) to calculate the total number of pages, the other is CollUtil.page (index, records per page, data list) to return the specified paging results, note that the index here starts from 1, and the SpringDataJPA paging index starts from 0.
/ * my order-to be paid [pagination] * * @ param openid user unique ID * @ return result set * / public Map findMyOrderNotPay (String openid, Integer pageIndex, Integer pageSize) {Map map = new HashMap (); / / query List orderList = consultOrderRepository.findMyOrderNotPay (openid); if (CollectionUtils.isEmpty (orderList)) {map.put ("pageIndex", pageIndex.toString ()) / / current page map.put ("pageSize", pageSize.toString ()); / / number of entries per page map.put ("total", "0"); / / Total number of records map.put ("pages", "0"); / / Total pages map.put ("list", new ArrayList ()); / / data list return map } List pageList = new ArrayList (); int totalSize = 0; int totalPage = 0; / calculate the total number of pages totalSize = orderList.size (); totalPage = PageUtil.totalPage (totalSize, pageSize); / / pagination. The list is returned only if the index is less than or equal to the total number of pages. If (pageIndex
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.