通过Structs+Hibernate+MySQL数据数来实现分页技术的应用.
源程序下载
至于Hibernate, Structs, MySQL的具体用法我就不说了,你可以看源程序来查看详细内容.我们来看看分页应用的相关内容.
架构上和Spring JPetStore没多少区别, 只是没用Spring框架所以Structs中的UserdisplayAction中必须new出各个对象并完成它们之间的关联, 这就是Spring IOC可自动完成的功能. 它也是三层:表现层(Structs), 业务层(POJO), 数据层(Hibernate访问MySQL). 同样业务层通过DAO接口(UserDAO)来访问数据库, 访问数据库的通过Hibernate(在UserDAOImpl中实现). 这一次分页要在业务层中来实现, 业务层同样向表现层提供一个Facada(门面, UserManager接口), 来访问业务. 在UserManagerImpl中通过调用UserDAO来访问数据, 调用POJO来实现分页功能. 看以下关键代码.
先看POJO的分布类吧:
Page.java
package org.flyware.util.page;
public class Page {
private boolean hasPrePage;
private boolean hasNextPage;
private int everyPage;
private int totalPage;
private int currentPage;
private int beginIndex;
public Page(){
}
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
}
......
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
PageUtil.java(用来构造Page实例 相当于page工厂)
package org.flyware.util.page;
public class PageUtil {
private static final Log logger = LogFactory.getLog(PageUtil.class);
public static Page createPage(Page page, int totalRecords){
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}
public static Page createPage(int everyPage, int currentPage, int totalRecords){
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage,
everyPage, totalPage,
currentPage, beginIndex);
}
private static int getEveryPage(int everyPage){
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage){
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage){
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords){
int totalPage = 0;
if(totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1 ;
return totalPage;
}
private static boolean hasPrePage(int currentPage){
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage){
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
业务实现类 UserManaerImpl.java
package com.linan.service.impl;
public class UserManagerImpl implements UserManager {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
//提供业务方法
public Result listUser(Page page) throws HibernateException,
ObjectNotFoundException {
int totalRecords = userDAO.getUserCount();
if(totalRecords == 0)
throw new ObjectNotFoundException("userNotExist", null);
page = PageUtil.createPage(page, totalRecords);
List users = userDAO.getUserByPage(page);
return new Result(page, users);
}
}
DAO数据访问实现类 UserDAOImpl.java
package com.linan.dao.impl;
public class UserDAOImpl implements UserDAO {
...........
public List getUserByPage(Page page) throws HibernateException {
String querySentence = "FROM user in class com.linan.bo.Userlist";
Query query = getSession().createQuery(querySentence);
//实现分页功能
query.setFirstResult(page.getBeginIndex())
.setMaxResults(page.getEveryPage());
return query.list();
}
}
表现层Structs中的Action UserdisplayAction.java
package com.linan.struts.action;
public class UserdisplayAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
Page page=new Page();
UserManagerImpl userManager=new UserManagerImpl();
UserDAOImpl userDAO=new UserDAOImpl();
userManager.setUserDAO(userDAO);
//掉用业务代理完成分页
Result result=userManager.listUser(page);
List list=result.getContent();
HttpSession session=request.getSession();
session.setAttribute("list",list);
return mapping.findForward("displayGo");
}
}