系统的整理了一下分页查询,如果有需要lib包的可以评论留邮箱。
pojo层
package pojo;
public class Xs {
private int id;
private String xh;
private String name;
private String age;
private String sex;
private String major;
private String college;
private String introduction;
private String photo;public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
hbm.xml配置
<?xml version="1.0" encoding="GB18030"?>
<hibernate-mapping package="pojo">
<class name="Xs" table="student">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="xh" column="xh" />
<property name="name" column="name" />
<property name="age" column="age" />
<property name="sex" column="sex" />
<property name="major" column="major" />
<property name="college" column="college" />
<property name="introduction" column="introduction" />
<property name="photo" column="photo" />
</class>
</hibernate-mapping>
dao、daoimpl层
package dao;
import java.util.List;
import pojo.Xs;public interface XsDAO {
public int findCount();
public List<Xs> findByPage(int begin, int pageSize);
}
package dao.impl;
import dao.XsDAO;
import pojo.Xs;
public class XsDAOImpl extends HibernateTemplate implements XsDAO{
List<Xs> xss;
public List<Xs> getXss() {
return xss;
}
public void setXss(List<Xs> xss) {
this.xss = xss;
}
@Override
public int findCount() {
Criteria criteria = getSession().createCriteria(Xs.class);
criteria.setProjection(Projections.rowCount());
Long count = (Long) criteria.uniqueResult();
int a=count.intValue();
System.out.println("条件个数"+a);
return a;
}
@Override
public List<Xs> findByPage(int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(Xs.class);
List<Xs> list = this.findByCriteria(criteria, begin, pageSize);
return list;
}
}
service,serviceimpl
package service;
import pojo.Xs;
import com.PageBean;
public interface XsService {
public PageBean<Xs> findByPage(Integer currPage);
}
package service.impl;
import com.PageBean;
import dao.XsDAO;import service.XsService;
public class XsServiceImpl implements XsService{
XsDAO xsDAO;
public XsDAO getXsDAO() {
return xsDAO;
}
public void setXsDAO(XsDAO xsDAO) {
this.xsDAO = xsDAO;
}
@Override
public PageBean<Xs> findByPage(Integer currPage) {
PageBean<Xs> pageBean = new PageBean<Xs>();
//封装当前的页数:
pageBean.setCurrPage(currPage);
//封装每页显示记录数
int pageSize = 5;
pageBean.setPageSize(pageSize);
//封装总记录数:
int totalCount =xsDAO.findCount();
pageBean.setTotalCount(totalCount);
//封装总页数:
double tc = totalCount;
Double num = Math.ceil(tc / pageSize);
pageBean.setTotalPage(num.intValue());
//封装每页显示的数据:
int begin = (currPage - 1)*pageSize;
List<Xs> list = xsDAO.findByPage(begin,pageSize);
pageBean.setList(list);
return pageBean;
}
}
action
package action;
import service.XsService;
import com.PageBean;
public class XsAction {
XsService xsService;
public XsService getXsService() {
return xsService;
}
public void setXsService(XsService xsService) {
this.xsService = xsService;
}private Integer currPage = 1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
public String find(){
PageBean<Xs> pageBean = xsService.findByPage(currPage);
//将pageBean存入到值栈中
ActionContext.getContext().getValueStack().push(pageBean);
return "findXs";
}
}
pagebean封装页数
package com;
import java.util.List;
/**
* 分页Bean
*/
public class PageBean<T> {
private int currPage; //当前页数
private int pageSize; //每页显示的记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private List<T> list; //每页显示的数据
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}