一、导入mybatis和pagehelper坐标
< dependency>
< groupId> org. mybatis. spring. boot< / groupId>
< artifactId> mybatis- spring- boot- starter< / artifactId>
< version> 2.2 .0 < / version>
< / dependency>
< dependency>
< groupId> com. github. pagehelper< / groupId>
< artifactId> pagehelper- spring- boot- starter< / artifactId>
< version> 1.3 .0 < / version>
< / dependency>
二、配置数据库
spring:
main:
allow- circular- references: true
datasource:
druid:
driver- class - name: com. mysql. cj. jdbc. Driver
url: jdbc: mysql: / / localhost: 3306 / x_take_out? serverTimezone= Asia / Shanghai & useUnicode= true & characterEncoding= utf- 8 & zeroDateTimeBehavior= convertToNull& useSSL= false & allowPublicKeyRetrieval= true
username: root
password: root
三、编写代码实现分页
EmployeePageQueryDTO.java
@Data
public class EmployeePageQueryDTO implements Serializable {
//员工姓名
private String name;
//页码
private int page;
//每页显示记录数
private int pageSize;
}
EmployeeMapper.java
@Mapper
public interface EmployeeMapper {
Page < Employee > pageQuery ( EmployeePageQueryDTO employeePageQueryDTO) ;
}
EmployeeMapper.xml
< select id= "pageQuery" resultType= "top.weiyuexin.x.entity.Employee" >
select *
from employee
< where>
< if test= "name != null and name != ''" >
and name like concat ( '%' , #{ name} , '%' )
< / if >
< / where>
order by create_time desc
< / select>
EmployeeServiceImpl.java
@Override
public PageResult pageQuery ( EmployeePageQueryDTO employeePageQueryDTO) {
PageHelper . startPage ( employeePageQueryDTO. getPage ( ) , employeePageQueryDTO. getPageSize ( ) ) ;
Page < Employee > page = employeeMapper. pageQuery ( employeePageQueryDTO) ;
return new PageResult ( page. getTotal ( ) , page. getResult ( ) ) ;
}
四、接口测试