-
配置分页插件
@Configuration
public class MyBatisPlusConfig {
/**
* 分页插件配置
*
* @return PaginationInterceptor
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
-
自定义返回对象(可选,使用官方的
IPage
也可以)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponsePageBean {
/**
* 总条数
*/
private Long total;
/**
* 数据列表
*/
private List<?> data;
}
-
Controller
使用@RequestParam
注解获取url参数,当然从request
对象中拿也可以。
参数需要指定为 当前页
以及数目
,也就是一页多少条数据。
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {
@Autowired
private IEmployeeService employeeService;
@ApiOperation(value = "分页获取员工信息")
@GetMapping("/")
public ResponsePageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer start,
@RequestParam(defaultValue = "10") Integer size,
Employee employee,
LocalDate[] timeLimit) {
return employeeService.getEmployeeByPage(start, size, employee, timeLimit);
}
}
-
service
public interface IEmployeeService extends IService<Employee> {
ResponsePageBean getEmployeeByPage(Integer start, Integer size, Employee employee, LocalDate[] timeLimit);
}
需要构建Page对象,作为参数传入Mapper,用以接收查询结果。
Page<Employee> page = new Page<>(start, size);
employeeMapper.getEmployeeByPage(page, employee, timeLimit)
IPage
对象参数:
-
getTotal() 总条数
-
getRecords() 查询结果
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public ResponsePageBean getEmployeeByPage(Integer start, Integer size, Employee employee, LocalDate[] timeLimit) {
Page<Employee> page = new Page<>(start, size);
IPage<Employee> employeeIPage = employeeMapper.getEmployeeByPage(page, employee, timeLimit);
return new ResponsePageBean(employeeIPage.getTotal(), employeeIPage.getRecords());
}
}
-
Mapper
Mapper对象要返回IPage
对象,其中包含了查询结果以及辅助参数(条数,结果等等),所以,这个也可以作为Controller返回对象,但其中很多参数实际上用处不大,选择精简也可以。
public interface EmployeeMapper extends BaseMapper<Employee> {
IPage<Employee> getEmployeeByPage(Page<Employee> page, @Param("employee") Employee employee, @Param("timeLimit") LocalDate[] timeLimit);
}