后台代码实现:
1、创建分页相关的实体类
public class Pagination {
private Integer page;
private Integer rows;
private Integer start;
public Pagination() {}
public Pagination(Integer page,Integer rows) {
this.page = page;
this.rows = rows;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
public Integer getStart() {
start = (page-1)*rows;
return start;
}
}
其中page为第几页,rows是一页的大小,start表示从一页的哪个位置开始;
2、写相关的mapper接口和xml
首先先统计数据库存储数据的总条数
public int findAllEmployee()throws Exception;
在xml中写如下代码:
2.1 统计总记录数
<select id="findAllEmployee" parameterType="cn.bgi.pojo.Pagination" resultType="Integer">
select count(eno) from t_employee
</select>
2.2 统计完总记录数后,使用sql语句划分每一页显示数据的多少
<select id="findEmployeeByPage" parameterType="cn.bgi.pojo.Pagination" resultType="cn.bgi.pojo.Employee">
select * from t_employee limit #{arg0},#{arg1}
</select>
3、在service层中写相关逻辑
4、在controller层中进行前后端交互
@RequestMapping(value = "/get", method = RequestMethod.GET)
public DataResult<Employee> get(String ename,Integer page,Integer rows) throws Exception {
if(ename!=null) {
List<Employee> list2 = employeeService.findEmployee(ename);
return new DataResult<Employee>(list2.size(), list2);
}else {
//分页的实现
Pagination pagination = new Pagination();
pagination.setPage(page);
pagination.setRows(rows);
List<Employee> list1 = employeeService.findEmployeeByPage(pagination.getStart(),rows);
return new DataResult<Employee>(employeeService.findAllEmployee(), list1);
}
}
前端代码实现:
使用datagrid的分页属性进行分页操作
$(function(){
$('#dg').edatagrid({
method : 'get',
url : '/ssm/employee/get.do',
//updateUrl: '/ssm/employee/update.do',
idField:'eno',
fit:true,
fitColumns: true,
pagination:true,
pageSize:3,
pageList:[3,6,12],
})
})