参考连接:
https://blog.csdn.net/weixin_39616547/article/details/112281931
https://mp.baomidou.com/guide/page.html
mybatis-plus自带分页插件,无需额外导入
1、配置
package com.kang.staffinfosystem.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.kang.staffinfosystem.mapper")
public class MybatisPlusConfig {
//配置分页插件
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
2、mapper中使用
修改前:
List<AllStaff> getAllStaffs();
修改后:
IPage<List<AllStaff>> getAllStaffs(Page<?> page);
如果只获取指定页数据,不需要页数等信息可以使用这个:
List<AllStaff> getAllStaffs(Page<?> page);
3、service接口
扫描二维码关注公众号,回复:
13381067 查看本文章
IPage<List<AllStaff>> getAllStaffs(Page<?> page);
4、service实现
public IPage<List<AllStaff>> getAllStaffs(Page<?> page) {
return allStaffMapper.getAllStaffs(page);
}
4、controller中
@PostMapping("/allStaff")
public Map<String, Object> getAllStaffs(Integer page,Integer limit){
IPage<List<AllStaff>> iPage = staffService.getAllStaffs(new Page<>(page,limit));
Map<String,Object> map = new HashMap<>();
map.put("code",0);
map.put("data",iPage.getRecords());
map.put("msg","返回成功");
map.put("count",iPage.getTotal());
return map;
}