从数据库层面分页(数据库为mySQL,数据访问层框架为mybatis)
package com.pagehelper.test;
import java.util.List;
public class PageBean<T> {
/*总记录条数*/
private int totalRecord;
/*总页数*/
private int totalPage;
/*每一页数据条数*/
private int pageSize;
/*当前页码*/
private int pageNow;
/*获取到的数据集合泛型为了使工具类具有通用性*/
private List<T> list;
/*数据库查询从那条记录开始*/
private int startIndex;
public PageBean(int pageNow, int pageSize, int totalRecord){
this.totalRecord = totalRecord;
/*三目运算,totalRecord能被pageSize整除totalPage=totalRecord/pageSize
* 不能整除totalPage=totalRecord/pageSize+1*/
totalPage = totalRecord%pageSize==0?totalRecord/pageSize:totalRecord/pageSize+1;
this.pageSize = pageSize;
this.pageNow = pageNow;
startIndex = (pageNow-1)*pageSize;
}
/*固定PageSize的构造器*/
public PageBean(int pageNow, int totalRecord){
this.totalRecord = totalRecord;
pageSize = 5;
totalPage = totalRecord%pageSize==0?totalRecord/pageSize:totalRecord/pageSize+1;
this.pageNow = pageNow;
startIndex = (pageNow-1)*pageSize;
}
/*Set,Get方法*/
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
}
Mapper接口最低需要两个方法,一个方法查询总记录数,另一个查数据
Worker是我数据库对于的pojo类。
int getTotalRecord();
List<Worker> selectAll(@Param(value = "startIndex") int startIndex, @Param(value = "pageSize")int pageSize);
下面代码是Worker对应得resultMap和接口方法对应得实现
<mapper namespace="com.pagehelper.mapper.WorkerMapper" >
<resultMap id="BaseResultMap" type="com.pagehelper.pojo.Worker" >
<id column="wid" property="wid" jdbcType="INTEGER" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="company" property="company" jdbcType="VARCHAR" />
<result column="department" property="department" jdbcType="VARCHAR" />
<result column="job" property="job" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="tel" property="tel" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="regist_time" property="registTime" jdbcType="TIMESTAMP" />
<result column="img" property="img" jdbcType="VARCHAR" />
resultMap>
<select id="getTotalRecord" resultType="int">
SELECT COUNT(wid) FROM worker
select>
<select id="selectAll" resultMap="BaseResultMap">
SELECT wid, password, company, department,job, name, sex, age, tel, email, regist_time,img FROM worker limit #{startIndex},#{pageSize}
select>
mapper>
Service 层调用代码:
@Override
public PageBean<Worker> getAll(int pageNow) {
/*先调用获取总记录数的方法getTotalRecord()*/
int totalRecord = workerMapper.getTotalRecord();
/*实例化一个工具类PageBean 传入参数pageNow,totalRecord,我这里用的PageSize固定的构造器*/
PageBean<Worker> pageBean = new PageBean<>(pageNow,totalRecord);
/*调用查询数据的方法selectAll(StartIndex,PageSize),参数使用PageBean的属性,查询的结果封装到PageBean的List中*/
pageBean.setList(workerMapper.selectAll(pageBean.getStartIndex(), pageBean.getPageSize()));
/*返回PageBean对象*/
return pageBean;
}
因为建的java项目,没有写Controller和页面,所以用测试方法展示查询结果
package com.pagehelper.test;
import com.pagehelper.mapper.WorkerMapper;
import com.pagehelper.pojo.Worker;
import com.pagehelper.service.impl.WorkerServiceImpl;
import com.pagehelper.util.PageBean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class Test1 {
public static void main(String[] args) throws IOException {
/*获取,加载配置文件*/
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
/*创建SqlSessionFactory 会话工厂*/
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
/*生产SQLSession*/
SqlSession session = sessionFactory.openSession();
/*获得WorkerMapper接口的实现*/
WorkerMapper mapper = session.getMapper(WorkerMapper.class);
/*Controller层调用Service层方法,实例化WorkerService*/
WorkerServiceImpl workerService = new WorkerServiceImpl(mapper);
/*调用WorkerService方法,返回PageBean,封装了分页相关数据
* Controller拿到数据之后用request或module将PageBean传递到前端页面,页面通过EL表达式可以直接获取数据库数据,当前页码,总页码等相关参数*/
PageBean<Worker> pageBean = workerService.getAll(1);
/*打印总记录数TotalRecord*/
System.out.println("总记录数:"+pageBean.getTotalRecord());
/*打印总页数TotalPage*/
System.out.println("总页数:"+pageBean.getTotalPage());
/*打印从数据库获取的数据*/
for (Worker worker:pageBean.getList()) {
System.out.println(worker);
}
}
}
运行结果
log4j:ERROR Could not find value for key log4j.appender.LogFIle
log4j:ERROR Could not instantiate appender named "LogFIle".
2019-07-24 09:26:41 [ main:1 ] - [ DEBUG ] ==> Preparing: SELECT COUNT(wid) FROM worker
2019-07-24 09:26:41 [ main:56 ] - [ DEBUG ] ==> Parameters:
2019-07-24 09:26:41 [ main:123 ] - [ DEBUG ] <== Total: 1
2019-07-24 09:26:41 [ main:131 ] - [ DEBUG ] ==> Preparing: SELECT wid, password, company, department,job, name, sex, age, tel, email, regist_time,img FROM worker limit ?,?
2019-07-24 09:26:41 [ main:132 ] - [ DEBUG ] ==> Parameters: 0(Integer), 5(Integer)
2019-07-24 09:26:41 [ main:148 ] - [ DEBUG ] <== Total: 5
总记录数:29
总页数:6
Worker{wid=100001, password='admin', company='软帝', department='开发部', job='超级管理员', name='超级管理员', sex='男', age=18, tel='123', email='[email protected]', registTime=Sat Jul 20 11:33:18 CST 2019, img='default.jpg'}
Worker{wid=100002, password='123', company='软帝集团', department='开发部', job='职员', name='管理员', sex='男', age=122, tel='123', email='[email protected]', registTime=Sat Jul 20 16:00:59 CST 2019, img='default.jpg'}
Worker{wid=100003, password='admin', company='软帝', department='开发部', job='管理员', name='管理员', sex='男', age=18, tel='123', email='[email protected]', registTime=Sat Jul 20 11:33:20 CST 2019, img='default.jpg'}
Worker{wid=100004, password='123', company='软帝', department='开发部', job='BOSS', name='123', sex='男', age=18, tel='123', email='123', registTime=Sat Jul 20 15:10:33 CST 2019, img='default.jpg'}
Worker{wid=100005, password='123', company='软帝集团', department='人事部', job='经理', name='萨达', sex='男', age=18, tel='123', email='123', registTime=Sat Jul 20 15:10:50 CST 2019, img='default.jpg'}
Process finished with exit code 0