1.设计分页实体(pageBean)
这里我显示的是3-12页的方式:
package cn.itcast.oa.domain; import java.util.List; /**
* 封装分页信息
* @author zhaoqx
*
*/
public class PageBean {
/**从页面提交过来的参数**/
private int currentPage;//----当前页码
private int pageSize;//-------每页显示多少条数据 /**查询数据库获得**/
private int recordCount;//----总记录数
private List recordList;//页面要显示的数据集合 /**由上面4个计算获得**/
private int pageCount;//------总页数
private int beginPageIndex;//-开始页码
private int endPageIndex;//---结束页码 public PageBean() {} public PageBean(int currentPage, int pageSize, int recordCount,List recordList) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.recordCount = recordCount;
this.recordList = recordList; pageCount = (this.recordCount + this.pageSize - 1) / this.pageSize;//计算页数 if(pageCount <= 10){
this.beginPageIndex = 1;
this.endPageIndex = this.pageCount;
}else{
this.beginPageIndex = this.currentPage - 4;
this.endPageIndex = this.currentPage + 5; if(this.beginPageIndex < 1){
this.beginPageIndex = 1;
this.endPageIndex = 10;
}
if(this.endPageIndex > this.pageCount){
this.endPageIndex = this.pageCount;
this.beginPageIndex = this.endPageIndex - 9;
}
}
} public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public int getPageCount() {
return pageCount;
} public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getRecordCount() {
return recordCount;
} public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
} public int getBeginPageIndex() {
return beginPageIndex;
} public void setBeginPageIndex(int beginPageIndex) {
this.beginPageIndex = beginPageIndex;
} public int getEndPageIndex() {
return endPageIndex;
} public void setEndPageIndex(int endPageIndex) {
this.endPageIndex = endPageIndex;
} public List getRecordList() {
return recordList;
} public void setRecordList(List recordList) {
this.recordList = recordList;
}
}
2.在action里面调用
package cn.itcast.oa.action; import java.util.Date;
import java.util.List; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.domain.Forum;
import cn.itcast.oa.domain.PageBean;
import cn.itcast.oa.domain.Reply;
import cn.itcast.oa.domain.Topic;
import cn.itcast.oa.utils.HQLHelper; /**
* 主题操作
* @author zhaoqx
*
*/
@Controller
@Scope("prototype")
public class TopicAction extends BaseAction<Topic>{
private Long forumId;//属性驱动,版块id /**
* 显示单个主题(回复列表)
*/
public String show(){
//根据id查询主题
Topic topic = topicService.getById(model.getId());
getValueStack().push(topic); //根据主题查询对应的回复列表
HQLHelper hh = new HQLHelper(Reply.class);
hh.addWhere("o.topic = ?", model);
hh.addOrderBy("o.postTime", true);
PageBean pb = replyService.getPageBean(hh,currentPage);
getValueStack().push(pb); return "show";
} public void setForumId(Long forumId) {
this.forumId = forumId;
} public Long getForumId() {
return forumId;
}
}
这里的currentPage 是在baseAction里面定义的 代码如下:
1 protected int currentPage = 1;//属性驱动,当前页码
2
3 public int getCurrentPage() {
4 return currentPage;
5 }
6
7 public void setCurrentPage(int currentPage) {
8 this.currentPage = currentPage;
9 }
3.在BaseDaoImpl 里面写
package cn.itcast.oa.base; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List; import javax.annotation.Resource; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate; import cn.itcast.oa.domain.Book;
import cn.itcast.oa.domain.PageBean;
import cn.itcast.oa.utils.HQLHelper;
/**
* 通用Dao实现
* @author zhaoqx
*
* @param <T>
*/
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T> implements IBaseDao<T> {
@Resource
private SessionFactory sessionFactory; private Class<T> clazz; public BaseDaoImpl() {
//获得实体类型
ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();//获得真正的父类
Type[] types = genericSuperclass.getActualTypeArguments();
clazz = (Class<T>) types[0];
} public void save(T entity) {
getSession().save(entity);
} public void delete(Long id) {
getSession().delete(getSession().get(clazz, id));
} public void update(T entity) {
getSession().update(entity);
} public List<T> findAll() {
String hql = "FROM " + clazz.getSimpleName();
return getSession().createQuery(hql).list();
} public T getById(Long id) {
return (T) getSession().get(clazz, id);
} public List<T> getByIds(Long[] ids) {
String hql = "FROM " + clazz.getSimpleName() + " WHERE id in (:ids)";
Query query = getSession().createQuery(hql);
query.setParameterList("ids", ids);//一次赋值多个
return query.list();
} public Session getSession(){
return sessionFactory.getCurrentSession();
} /**
* 公共分页
*/
public PageBean getPageBean(HQLHelper hh, int currentPage) {
int pageSize = 5;
int firstResult = (currentPage - 1) * pageSize;
String listHQL = hh.getListHQL();
String countHQL = hh.getCountHQL();
List<Object> args = hh.getArgs(); Query query = this.getSession().createQuery(listHQL);
if(args != null && args.size() > 0){
int index = 0;
for(Object o : args){
query.setParameter(index++, o);
}
}
query.setFirstResult(firstResult);
query.setMaxResults(pageSize);
List recordList = query.list(); query = this.getSession().createQuery(countHQL);
if(args != null && args.size() > 0){
int index = 0;
for(Object o : args){
query.setParameter(index++, o);
}
}
Long recordCount = (Long) query.uniqueResult(); return new PageBean(currentPage, pageSize, recordCount.intValue(), recordList);
} }
4.最后还有一个工具类:
package cn.itcast.oa.utils; import java.util.ArrayList;
import java.util.List; /**
* 辅助生成HQL语句的工具类
* @author zhaoqx
*
*/
public class HQLHelper {
private String fromStr;//FROM 子句
private String whereStr = "";//WHERE 子句
private String orderByStr = "";//ORDER BY 子句 private List<Object> args = new ArrayList<Object>();//封装HQL中对应的参数信息 public HQLHelper() {} /**
* 通过构造方法拼接FROM 子句
* @param clazz
*/
public HQLHelper(Class clazz) {
this.fromStr = "FROM " + clazz.getSimpleName() + " o ";
} /**
* 拼接WHERE 子句
* @param condition
* @param args
*/
public void addWhere(String condition,Object...args){//o.name = ?
if(this.whereStr.length()==0){
//第一次拼接WHERE子句
this.whereStr = " WHERE " + condition;
}else{
//不是第一次拼接WHERE子句
this.whereStr += " AND " + condition;
}
if(args != null && args.length > 0){
//封装参数
for(Object o : args){
this.args.add(o);
}
}
} /**
* 拼接ORDER BY 子句
* @param orderBy
* @param asc
*/
public void addOrderBy(String orderBy , boolean asc){
if(this.orderByStr.length() == 0){
//第一次拼接ORDER BY 子句
this.orderByStr = " ORDER BY " + orderBy + (asc ? " ASC " : " DESC ");
}else{
//不是第一次拼接ORDER BY 子句
this.orderByStr += ", " + orderBy + (asc ? " ASC " : " DESC ");
}
} /**
* 获取查询List集合的HQL语句
* @return
*/
public String getListHQL(){
return this.fromStr + this.whereStr + this.orderByStr;
} /**
* 获取查询统计记录数的HQL
* @param args
*/
public String getCountHQL(){
return "SELECT COUNT(*) " + this.fromStr + this.whereStr;
} public void setArgs(List<Object> args) {
this.args = args;
} public List<Object> getArgs() {
return args;
} }
好了,这样一个通用的分页就完成啦。。。