在mapper.xml文件添加sql语句
<select id="getEmpByLimit" parameterType="map" resultType="Emp">
select * from emp order by empno limit #{startIndex},#{pageSize}
select>
在接口中添加方法:
List<Emp> getEmpByLimit(Map<String, Integer> map);
在测试类中添加:
int startIndex= (currentPage - 1)* pageSize; //根据 当前页 计算起始位置
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",startIndex);//起始位置
map.put("pageSize",pageSize);//每页条数
List<Emp> empList = mapper.getEmpByLimit(map);
for (Emp emp : empList) {
System.out.println(emp);
}
在mapper.xml文件添加sql语句
<select id="getEmpByBounds" resultType="Emp">
select * from emp order by empno
select>
在接口中添加方法:
List<Emp> getEmpByBounds(RowBounds rowBounds);
在测试类中添加:
int n = 1; //表示从第几页数据开始
int pageSize = 5;//连续取出几条数据
int start = (n - 1)* pageSize; //n 当前页
RowBounds rowBounds = new RowBounds(start, pageSize );
List<Emp> list = mapper.getEmpByBounds(rowBounds);
for (Emp user : list) {
System.out.println(user);
}
pom文件中添加依赖
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>4.1.6version>
dependency>
<dependency>
<groupId>com.github.jsqlparsergroupId>
<artifactId>jsqlparserartifactId>
<version>0.9.5version>
dependency>
mybatis配置文件中添加插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
plugin>
plugins>
在mapper.xml文件添加sql语句
<select id="getEmpByPageHelper" resultType="Emp">
select * from emp order by empno
select>
在接口中添加方法:
List<Emp> getEmpByPageHelper();
在测试类中添加:
Page<Object> startPage = PageHelper.startPage(1, 5);
List<Emp> empList = mapper.getEmpByPageHelper();
for (Emp emp : empList) {
System.out.println(emp);
}
System.out.println("总条数"+startPage.getTotal());
System.out.println("当前页码"+startPage.getPageNum());
System.out.println("每页的记录数"+startPage.getPageSize());
System.out.println("总页码"+startPage.getPages());
//用PageInfo对结果进行包装
PageInfo<Emp> page = new PageInfo<Emp>(empList);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
System.out.println("当前页码"+ page.getPageNum()) ; //
System.out.println("每页的记录数"+ page.getPageSize()) ; //
System.out.println("起始行"+ page.getStartRow()) ; //
System.out.println("尾行"+ page.getEndRow() ) ; //
System.out.println("总条数"+ page.getTotal() ) ;//
System.out.println("总页码"+ page.getPages() ) ; //
System.out.println("第一页"+ page.getFirstPage()) ; //
System.out.println("最后一页"+ page.getLastPage() ) ; //
System.out.println("是第一页"+ page.isIsFirstPage() ) ; //
System.out.println("是最后一页"+ page.isIsLastPage() ) ; //
System.out.println("有上一页"+ page.isHasPreviousPage() ) ; //
System.out.println("有下一页"+ page.isHasNextPage()) ; //
pom文件中添加依赖
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
在文件中加:
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/elm?characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
在controller中
PageHelper.startPage(1, 1);