1.PageBean 首先封装分页的基本信息
/**
* @author ZSQ 分页封装
*/
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;
}
}
2、 Action 类
/**
* @author ZSQ
* @description 部门管理Action类
*/
public class DepartAction extends ActionSupport implements
ModelDriven<Department> {
private Department department = new Department();
/*
* 提供模型驱动,封装部门信息类
*/
@Override
public Department getModel() {
// TODO Auto-generated method stub
return department;
}
// 注意此处向spring注入的是接口而不是实现类
private DepartServiceInte departService;
public void setDepartService(DepartServiceInte departService) {
this.departService = departService;
}
private Integer currPage = 1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
/*
* 查看所有部门信息
*/
public String findAll() {
PageBean<Department> pageBean = departService.findByPage(currPage);
ActionContext.getContext().getValueStack().push(pageBean);//保存分页列表到值栈
return "findAll";
}
}
3.Service类,设置每页的属性值
/**
* @author ZSQ
* @description 部门管理service接口实现类
*/
@Transactional
public class DepartService implements DepartServiceInte {
private DepartDaoInte departDao;
public void setDepartDao(DepartDaoInte departDao) {
this.departDao = departDao;
}
/*
* (non-Javadoc)
* @description 分页封装部门service接口实现,分页查询
*/
@Override
public PageBean<Department> findByPage(Integer currPage) {
// TODO Auto-generated method stub
PageBean<Department> pageBean = new PageBean<Department>();
// 封装当前页
pageBean.setCurrPage(currPage);
// 丰庄每页显示的记录数
int pageSize = 8;
pageBean.setPageSize(pageSize);
// 封装显示记录数
int totalCount = departDao.findCount();
pageBean.setTotalCount(totalCount);
// 封装总页数
double tc = totalCount;
Double num = Math.ceil(tc / pageSize);
pageBean.setTotalPage(num.intValue());
// 封装每页显示的数据
int begin = (currPage - 1) * pageSize;
List<Department> list = departDao.findByPage(begin, pageSize);
pageBean.setList(list);
return pageBean;
}
}
4、Dao类 数据库查询操作,查询总记录数,与分页查询
/**
* @author ZSQ
* @description 部门管理Dao实现类
*/
public class DepartDao extends HibernateDaoSupport implements DepartDaoInte {
/*
* (non-Javadoc) 查询部门记录数
*/
@SuppressWarnings("unchecked")
@Override
public int findCount() {
// TODO Auto-generated method stub
String hql = "select count(*) from Department";
Session session = getSessionFactory().getCurrentSession();
Query query = session.createQuery(hql);
List<Long> list = query.list();
if (list.size() > 0) {
return list.get(0).intValue();
}
return 0;
}
/*
* @按页查询
*/
@SuppressWarnings("unchecked")
@Override
public List<Department> findByPage(int begin, int pageSize) {
// TODO Auto-generated method stub
Session session = getSessionFactory().getCurrentSession();
DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
List<Department> list = (List<Department>) criteria
.getExecutableCriteria(session).setFirstResult(begin)
.setMaxResults(pageSize).list();
return list;
}
}
5、前端页面,迭代显示部门信息
<table class="table table-hover table-striped">
<thead>
<th>编号</th>
<th>部门名称</th>
<th>创建时间</th>
<th>部门人数</th>
<th>备注</th>
<th>操作</th>
</thead>
<tbody>
<s:iterator value="#request.list" id="department">
<tr>
<td>${department.id }</td>
<td>${department.departName }</td>
<td>${department.createtTime }</td>
<td>${department.employees.size()}</td>
<td>${department.description }</td>
<td><a
href="${pageContext.request.contextPath}/depart_edit.action?id=${department.id}">修改</a>
<a href="${pageContext.request.contextPath}/depart_del.action?id=${department.id}">删除</a>
</td>
</tr>
</s:iterator>
</tbody>
</table>
<!-- 分页显示 -->
<table border="0" cellspacing="0" cellpadding="5" width="900px">
<tr>
<td align="right">
<span>第<s:property value="currPage" />/<s:property
value="totalPage" />页
</span>
<span>总记录数:<s:property value="totalCount" /> 每页显示:<s:property
value="pageSize" />
</span>
<span>
<s:if test="currPage!=1">
<a href="${pageContext.request.contextPath}/depart_findAll.action?currPage=1">[首页]</a>
<a
href="${pageContext.request.contextPath}/depart_findAll.action?currPage=<s:property value="currPage-1"/>">[上一页]</a>
</s:if>
<s:if test="currPage !=totalPage">
<a
href="${pageContext.request.contextPath}/depart_findAll.action?currPage=<s:property value="currPage+1"/>">[下一页]</a>
<a
href="${pageContext.request.contextPath}/depart_findAll.action?currPage=<s:property value="totalPage"/>[尾页]</a>
</s:if>
</span>
</td>
<td align="right"><a href="addDepart.jsp">添加部门</a> <a href="index.jsp" style="color:green">返回登录首页</a></td>
</tr>
</table>
注意:由于部门与职工是一对多关系,而且要在部门列表显示职工信息,所以在部门bean里要封装职工类list或set,在职工bean里要封装部门类,同时要使用延迟加载
Department:
private Set<Employee> employees = new HashSet<Employee>();
Employee:
private Department department;