Letakkan semua kandungan yang diperlukan untuk paging ke dalam kelas entiti
Kelas entiti yang diperlukan untuk data paging! Mengandungi nombor halaman, saiz halaman, jumlah item, jumlah halaman, baris permulaan
pagehelpr menyediakan pageInfo kelas ini, kita tidak perlu menciptanya sendiri
pilih * dari baris permulaan had nama jadual, paparkan beberapa keping data
#Halaman n memaparkan lima keping data pada setiap halaman
pilih * dari had nama jadual ( n-1)*5,5
#Berapa banyak item yang dipaparkan pada setiap halaman Saiz halaman
3
#Berapa banyak item yang terdapat secara keseluruhan
jumlah
pilih kiraan(*) daripada nama jadual
#Jumlah bilangan halaman
halaman
pages=total%pagesSize ==0?total/pgeSize:total/pageSize+ 1;
#Halaman semasa
pageNum
1 pergantungan pagehelper
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
2. kelas entiti kacang
Entiti pengguna:
#pagehelper分页插件配置 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql
Kembalikan kelas entiti bahagian hadapan: termasuk data yang ditemui dan data halaman
rreee2.lapisan pemeta:
package com.qianfeng.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; @Data @ToString @AllArgsConstructor @NoArgsConstructor public class Register { private Integer id; private String userName; private String passWord; private String rePassWord; private String idCard; private String gender; }
3.lapisan perkhidmatan:
package com.qianfeng.bean; import com.github.pagehelper.PageInfo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.util.List; @Data @ToString @AllArgsConstructor @NoArgsConstructor public class RegPage { private PageInfo pageInfo; private List<Register> registers; }
4.serviceImpl:
package com.qianfeng.mapper; import com.qianfeng.bean.Register; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface PageDao { List<Register> getAll(Integer startRow,Integer pageSize); long getCount(); }
5.lapisan pengendali:
package com.qianfeng.service; import com.github.pagehelper.PageInfo; import com.qianfeng.bean.RegPage; public interface RegService { RegPage getAll(PageInfo pageInfo); }
6 .mapper.xml
package com.qianfeng.service.serviceImpl; import com.github.pagehelper.PageInfo; import com.qianfeng.bean.RegPage; import com.qianfeng.bean.Register; import com.qianfeng.mapper.PageDao; import com.qianfeng.service.RegService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class RegServiceImpl implements RegService { @Autowired private PageDao pageDao; @Override public RegPage getAll(PageInfo pageInfo) { List<Register> all = pageDao.getAll(pageInfo.getStartRow(), pageInfo.getPageSize());//分页后的数据 long count = pageDao.getCount();//总记录条数 pageInfo.setTotal(count); //总页数 int pages= (int) (pageInfo.getTotal()%pageInfo.getPageSize()==0?pageInfo.getTotal()/pageInfo.getPageSize():pageInfo.getTotal()/pageInfo.getPageSize()+1); pageInfo.setPages(pages); RegPage regPage = new RegPage(); regPage.setPageInfo(pageInfo); regPage.setRegisters(all); return regPage; } }
7 .application.yaml
package com.qianfeng.handler; import com.github.pagehelper.PageInfo; import com.qianfeng.bean.RegPage; import com.qianfeng.service.RegService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RegHandler { @Autowired private RegService regService; @RequestMapping("/page/{pageNum}") public RegPage regPage(@PathVariable("pageNum") Integer pageNum){ System.out.println("........."); PageInfo pageInfo = new PageInfo(); pageInfo.setPageNum(pageNum); pageInfo.setPageSize(3); pageInfo.setStartRow((pageNum-1)*pageInfo.getPageSize()); System.out.println("startRow" + pageInfo.getStartRow()); return regService.getAll(pageInfo); } }
8.application,properties
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qianfeng.mapper.PageDao"> <select id="getAll" resultType="com.qianfeng.bean.Register"> select * from m_register limit #{startRow},#{pageSize} </select> <select id="getCount" resultType="java.lang.Long"> select count(*) from m_register </select> </mapper>
Mengenai kelas PageInfo, kod sumber adalah seperti berikut:
spring: datasource: url: jdbc:mysql:///map?serverTimezone=Asia/Shanghai&useSSL=false username: root password: 123 driver-class-name: com.mysql.jdbc.Driver druid: aop-patterns: com.qianfeng.* #监控SpringBean filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙) stat-view-servlet: # 配置监控页功能 enabled: true login-username: admin login-password: admin resetEnable: false web-stat-filter: # 监控web enabled: true urlPattern: /* exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' filter: stat: # 对上面filters里面的stat的详细配置 slow-sql-millis: 1000 logSlowSql: true enabled: true wall: enabled: true config: drop-table-allow: false mvc: pathmatch: matching-strategy: ant_path_matcher # mybatis的配置规则 mybatis: #config-location: classpath:mapper/mybatis-config.xml mapper-locations: classpath:mapper/* configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # static-locations: [classpath:/haha/] # 静态资源路径自定义
Struktur direktori:
Atas ialah kandungan terperinci Cara menggunakan PageHelper dalam rangka kerja springboot+mybatis. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!