Datagrid分页查询
1.首先在项目引入通用mapper坐标:
<!--pagehelper:通用Mapper分页-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2.修改页面中Datagrid请求的路径和请求方式:
// 收派标准信息表格
$('#grid').datagrid( {
method : "get", //Restful中的查请求方式为get.
url : "/standard",
iconCls : 'icon-forward',
fit : true,
border : false,
rownumbers : true,
striped : true,
pageList: [3,5,100],
pagination : true,
toolbar : toolbar,
idField : 'id',
columns : columns
});
3.创建一个pojo类,目的是迎合Datagrid所需的返回参数的格式.
public class DataGridResult<T> { //T -- 泛型,代表的是传过来的是什么类型就返回什么类型
private Long total;
private List<T> rows;
/* get/set方法.....*/
}
4.查询功能:Controller
/**
* 分页查找
* @Param page:第几页数据
* @Param rows:每页条数
**/
@GetMapping
public ResponseEntity<DataGridResult<Standard>> queryStandard(Integer page,Integer rows){
try {
// 查询取派标准
DataGridResult<Standard> result = this.standardService.queryStandardByPage(page, rows);
if(result != null){
// 资源存在,返回200
return new ResponseEntity<>(result, HttpStatus.OK);
}
// 资源不存在,返回404
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} catch (Exception e) {
e.printStackTrace();
// 出现异常,返回500
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
-
查询功能:Service
public DataGridResult queryStandardByPage(Integer page, Integer rows) {
// 使用分页助手开始分页,指定两个参数:当前页码,每页条数
PageHelper.startPage(page, rows);
// 然后分页拦截器会自动对接下来的查询进行分页
List standards = this.standardMapper.select(null);// 不传查询条件
// 封装分页信息对象
PageInfo pageInfo = new PageInfo<>(standards);
// 封装页面数据对象
DataGridResult result = new DataGridResult(pageInfo.getTotal(), standards);
return result;
}