今天闲来无事,摆弄了一下分页,突然发现很多代码长时间不用就生梳了,虽然有些基础,但没有一篇整合的。这里还是简单示例,主要是以后自己翻着看不用百度找啊找找不到一篇想要的
1、PageBean实体类,一页出的内容全都有
package entity; import java.util.List; /**
* 定义一个分页对象
* @author 0
*
*/
public class PageBean<T> {
private int pageNo;//当前页码
private int totalPageCount;//总页码
private int totalCount;//总条数
private int pageSize=3;//每页显示条数
private int upPageNo;//上一页
private int nextPageNo;//下一页
//一页返回的数据集合
private List<?> list;
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
//如果当前页码大于0,才设置当前页码值
if(pageNo>0){
this.pageNo=pageNo;
}
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
if(this.getTotalCount()%this.pageSize==0){
this.totalPageCount=this.getTotalCount()/this.pageSize;
}else if(this.getTotalCount()%this.pageSize >0){
this.totalPageCount=this.getTotalCount()/this.pageSize +1;
}else{
this.totalPageCount = 0;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getUpPageNo() {
return upPageNo;
}
//对上一页进行判断
public void setUpPageNo(int upPageNo) {
//如果当前页>1
if(this.pageNo>1){
this.upPageNo = this.pageNo-1;
}
} public int getNextPageNo() {
return nextPageNo;
}
//对下一页进行判断
public void setNextPageNo(int nextPageNo) {
//如果当前页>0且小于总页数,则可以有下一页
if(this.pageNo>0 && this.pageNo < this.totalPageCount){
this.upPageNo = this.pageNo+1;
}
} public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
} }
2、dao层实现类,这里框架用的hibernate。hibernate作dao层的数据处理,真是太方便了
package dao.impl; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session; import util.HibernateSessionFactory; import dao.InfoMationDao;
import entity.Info;
import entity.PageBean; public class InfoMationDaoImpl implements InfoMationDao { public PageBean<Info> getInfoByPage(int pageNo, String where) {
Session session=HibernateSessionFactory.getSession();
PageBean<Info> pagebean=new PageBean<Info>(); try { //求数据总量
int totalCount = ((Number) session.createQuery("select count(*) as count from Info where 1=1 "+where).uniqueResult()).intValue() ;
//总数、当前页、总页数
pagebean.setTotalCount(totalCount);
pagebean.setPageNo(pageNo);
pagebean.setTotalPageCount(totalCount/pagebean.getPageSize());
pagebean.setUpPageNo(pageNo-1);
pagebean.setNextPageNo(pageNo+1); //建立查询
Query query = session.createQuery("from Info where 1=1 "+where);
//设置起始行pageSize*pageNo,(pageNo-1)*pageSize
query.setFirstResult((pageNo-1)*pagebean.getPageSize());
//设置每页条数
query.setMaxResults(pagebean.getPageSize());
List<Info> infolist = query.list();
pagebean.setList(infolist); } catch (Exception e) {
e.printStackTrace();
}
return pagebean;
} /* public static void main(String[] args) {
InfoMationDao im=new InfoMationDaoImpl();
int pageno=1;
String where="";
PageBean<Info> pb = im.getInfoByPage(pageno, where);
System.out.println(pb.getList().size());
}*/
}
so easy!