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 = ;
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 = ;
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 - ) * 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() > ) {
return list.get().intValue();
}
return ;
}
/*
* @按頁查詢
*/
@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;