pageHelper 一般分页
加入依赖
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
配置 springboot
mybatis:
type-aliases-package: com.xxxxxxx # 搜索指定包别名
mapper-locations: classpath:mapper/**/*Mapper.xml # 配置mapper的扫描
configuration:
map-underscore-to-camel-case: true
pagehelper:
reasonable: true
support-methods-arguments: true
params: count=countSql
helper-dialect: mysql
配置Mapper.xml
<select id="selectPage" resultMap="SysUserResultMap">
select id,username from sys_user
</select>
配置请求数据实体
@Data
public class PageRequest {
private int pageNum;
private int pageSize;
}
封装工具
public class PageUtil {
public static PageResult getPageResult(PageRequest pageRequest, PageInfo<?> pageInfo) {
PageResult pageResult = new PageResult();
pageResult.setPageNum(pageInfo.getPageNum());
pageResult.setPageSize(pageInfo.getPageSize());
pageResult.setTotalSize(pageInfo.getTotal());
pageResult.setTotalPages(pageInfo.getPages());
pageResult.setContent(pageInfo.getList());
return pageResult;
}
}
配置返回数据实体
@Data
public class PageResult {
private int pageNum;
private int pageSize;
private long totalSize;
private int totalPages;
private List<?> content;
}
业务层配置
Mapper
@Repository
public interface SysUserMapper {
public List<SysUser> list();
}
接口
public interface ISysUserService {
PageResult list(PageRequest pageRequest);
}
实现接口
@Service
public class SysUserServiceImpl implements ISysUserService {
@Autowired
private SysUserMapper userMapper;
private PageInfo<SysUser> getPageInfo(PageRequest pageRequest) {
int pageNum = pageRequest.getPageNum();
int pageSize = pageRequest.getPageSize();
PageHelper.startPage(pageNum, pageSize);
List<SysUser> sysMenus = userMapper.selectPage();
return new PageInfo<SysUser>(sysMenus);
}
@Override
public PageResult list(PageRequest pageRequest) {
return PageUtil.getPageResult(pageRequest, getPageInfo(pageRequest));
}
}
控制器,装入数据列表测试
@GetMapping("/system/user/list")
@ResponseBody
public Object findPage(PageRequest pageQuery) {
return userService.list(pageQuery);
}