1.Mybatis-plus拦截器配置
(该配置文件位置随便放)
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//向Mybatis过滤器链中添加分页拦截器
interceptor.addInnerInterceptor(new
PaginationInnerInterceptor(DbType.POSTGRE_SQL));
//还可以添加其它的拦截器
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
2.Service接口
public interface WorkonsecondmentService extends IService<Workonsecondment>{
/**
* 分页查询数据
* @param CurrentPage
* @param PageSize
* @return
*/
Page<Workonsecondment> getworkByPage(Integer CurrentPage,Integer PageSize);
}
3.Service接口实现类
@Override
public Page<Workonsecondment> getworkByPage(Integer currentPage, Integer pageSize) {
Page<Workonsecondment> page=new Page<>(currentPage,pageSize);
wkmapper.getWorkByPage(page);
return page;
}
4.Mapper接口
@Mapper
public interface WorkonsecondmentMapper extends BaseMapper<Workonsecondment> {
/**
* 分页返回数据
* @param page
* @return
*/
Page<Workonsecondment> getWorkByPage(Page<Workonsecondment> page);
}
5.Mapper接口对应xml文件
<select id="getWorkByPage" resultType="Workonsecondment">
select * from WORKONSECONDMENT
</select>
文件里面的resultType是对应实体类的类型
Controller层测试
@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
public CommonResult getWorkByPage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
@RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
Page<Workonsecondment> page = wkds.getworkByPage(currentPage, pageSize);
List<Workonsecondment> records = page.getRecords();
for (Workonsecondment record : records) {
System.out.println(record);
}
System.out.println(page);
return CommonResult.success(page, "获取成功");
}
CommonResult是我自己封装的数据统一格式。