实现分页查询
实体类
package com.approval.bean;
import java.io.Serializable;
import java.util.List;
import org.apache.poi.ss.formula.functions.T;
public class Page<T> implements Serializable{
private static final long serialVersionUID = 1L;
//每页显示多少条:默认10条
private int pageSize = 10;
//总页数
private int pageTotal;
//当前第多少页:默认第1页
private int pageNum = 1;
//总共多少条
private int totalCount;
//要显示的数据
private List<T> dataList;
//分页方法
public Page(int pageNumumber, int pageSize, List<T> sourceList){
if(sourceList == null || sourceList.isEmpty()){
return;
}
// 总记录条数
this.totalCount = sourceList.size();
// 每页显示多少条记录
this.pageSize = pageSize;
//获取总页数
this.pageTotal = this.totalCount / this.pageSize;
if(this.totalCount % this.pageSize !=0){
this.pageTotal = this.pageTotal + 1;
}
// 当前第几页数据
this.pageNum = this.pageTotal < pageNum ? this.pageTotal : pageNum;
// 起始索引
int fromIndex = this.pageSize * (this.pageNum -1);
// 结束索引
int toIndex = this.pageSize * this.pageNum > this.totalCount ? this.totalCount : this.pageSize * this.pageNum;
this.dataList = sourceList.subList(fromIndex, toIndex);
}
public Page(){
}
public Page(int pageSize,int pageNum,int totalCount,int pageTotal,List<T> dataList) {
super();
this.pageSize = pageSize;
this.pageNum = pageNum;
this.totalCount = totalCount;
this.pageTotal = pageTotal;
this.dataList = dataList;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageTotal() {
return pageTotal;
}
public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
}
配置.xml文件:UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.approval.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.approval.bean.User">
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_code" property="userCode" jdbcType="VARCHAR" />
<result column="user_n