SSH2 框架下的分页
Posted 草鸟It
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH2 框架下的分页相关的知识,希望对你有一定的参考价值。
1.设计分页实体(pageBean)
这里我显示的是3-12页的方式:
1 package cn.itcast.oa.domain; 2 3 import java.util.List; 4 5 /** 6 * 封装分页信息 7 * @author zhaoqx 8 * 9 */ 10 public class PageBean { 11 /**从页面提交过来的参数**/ 12 private int currentPage;//----当前页码 13 private int pageSize;//-------每页显示多少条数据 14 15 /**查询数据库获得**/ 16 private int recordCount;//----总记录数 17 private List recordList;//页面要显示的数据集合 18 19 /**由上面4个计算获得**/ 20 private int pageCount;//------总页数 21 private int beginPageIndex;//-开始页码 22 private int endPageIndex;//---结束页码 23 24 public PageBean() {} 25 26 27 28 public PageBean(int currentPage, int pageSize, int recordCount,List recordList) { 29 this.currentPage = currentPage; 30 this.pageSize = pageSize; 31 this.recordCount = recordCount; 32 this.recordList = recordList; 33 34 pageCount = (this.recordCount + this.pageSize - 1) / this.pageSize;//计算页数 35 36 if(pageCount <= 10){ 37 this.beginPageIndex = 1; 38 this.endPageIndex = this.pageCount; 39 }else{ 40 this.beginPageIndex = this.currentPage - 4; 41 this.endPageIndex = this.currentPage + 5; 42 43 if(this.beginPageIndex < 1){ 44 this.beginPageIndex = 1; 45 this.endPageIndex = 10; 46 } 47 if(this.endPageIndex > this.pageCount){ 48 this.endPageIndex = this.pageCount; 49 this.beginPageIndex = this.endPageIndex - 9; 50 } 51 } 52 } 53 54 public int getCurrentPage() { 55 return currentPage; 56 } 57 58 public void setCurrentPage(int currentPage) { 59 this.currentPage = currentPage; 60 } 61 62 public int getPageCount() { 63 return pageCount; 64 } 65 66 public void setPageCount(int pageCount) { 67 this.pageCount = pageCount; 68 } 69 70 public int getPageSize() { 71 return pageSize; 72 } 73 74 public void setPageSize(int pageSize) { 75 this.pageSize = pageSize; 76 } 77 78 public int getRecordCount() { 79 return recordCount; 80 } 81 82 public void setRecordCount(int recordCount) { 83 this.recordCount = recordCount; 84 } 85 86 public int getBeginPageIndex() { 87 return beginPageIndex; 88 } 89 90 public void setBeginPageIndex(int beginPageIndex) { 91 this.beginPageIndex = beginPageIndex; 92 } 93 94 public int getEndPageIndex() { 95 return endPageIndex; 96 } 97 98 public void setEndPageIndex(int endPageIndex) { 99 this.endPageIndex = endPageIndex; 100 } 101 102 public List getRecordList() { 103 return recordList; 104 } 105 106 public void setRecordList(List recordList) { 107 this.recordList = recordList; 108 } 109 }
2.在action里面调用
1 package cn.itcast.oa.action; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import org.springframework.context.annotation.Scope; 7 import org.springframework.stereotype.Controller; 8 9 import cn.itcast.oa.base.BaseAction; 10 import cn.itcast.oa.domain.Forum; 11 import cn.itcast.oa.domain.PageBean; 12 import cn.itcast.oa.domain.Reply; 13 import cn.itcast.oa.domain.Topic; 14 import cn.itcast.oa.utils.HQLHelper; 15 16 /** 17 * 主题操作 18 * @author zhaoqx 19 * 20 */ 21 @Controller 22 @Scope("prototype") 23 public class TopicAction extends BaseAction<Topic>{ 24 private Long forumId;//属性驱动,版块id 25 26 /** 27 * 显示单个主题(回复列表) 28 */ 29 public String show(){ 30 //根据id查询主题 31 Topic topic = topicService.getById(model.getId()); 32 getValueStack().push(topic); 33 34 //根据主题查询对应的回复列表 35 HQLHelper hh = new HQLHelper(Reply.class); 36 hh.addWhere("o.topic = ?", model); 37 hh.addOrderBy("o.postTime", true); 38 PageBean pb = replyService.getPageBean(hh,currentPage); 39 getValueStack().push(pb); 40 41 return "show"; 42 } 43 44 public void setForumId(Long forumId) { 45 this.forumId = forumId; 46 } 47 48 public Long getForumId() { 49 return forumId; 50 } 51 }
这里的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 里面写
1 package cn.itcast.oa.base; 2 3 import java.lang.reflect.ParameterizedType; 4 import java.lang.reflect.Type; 5 import java.util.List; 6 7 import javax.annotation.Resource; 8 9 import org.hibernate.Query; 10 import org.hibernate.Session; 11 import org.hibernate.SessionFactory; 12 import org.springframework.orm.hibernate3.HibernateTemplate; 13 14 import cn.itcast.oa.domain.Book; 15 import cn.itcast.oa.domain.PageBean; 16 import cn.itcast.oa.utils.HQLHelper; 17 /** 18 * 通用Dao实现 19 * @author zhaoqx 20 * 21 * @param <T> 22 */ 23 @SuppressWarnings("unchecked") 24 public class BaseDaoImpl<T> implements IBaseDao<T> { 25 @Resource 26 private SessionFactory sessionFactory; 27 28 private Class<T> clazz; 29 30 public BaseDaoImpl() { 31 //获得实体类型 32 ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();//获得真正的父类 33 Type[] types = genericSuperclass.getActualTypeArguments(); 34 clazz = (Class<T>) types[0]; 35 } 36 37 public void save(T entity) { 38 getSession().save(entity); 39 } 40 41 public void delete(Long id) { 42 getSession().delete(getSession().get(clazz, id)); 43 } 44 45 public void update(T entity) { 46 getSession().update(entity); 47 } 48 49 public List<T> findAll() { 50 String hql = "FROM " + clazz.getSimpleName(); 51 return getSession().createQuery(hql).list(); 52 } 53 54 public T getById(Long id) { 55 return (T) getSession().get(clazz, id); 56 } 57 58 public List<T> getByIds(Long[] ids) { 59 String hql = "FROM " + clazz.getSimpleName() + " WHERE id in (:ids)"; 60 Query query = getSession().createQuery(hql); 61 query.setParameterList("ids", ids);//一次赋值多个 62 return query.list(); 63 } 64 65 public Session getSession(){ 66 return sessionFactory.getCurrentSession(); 67 } 68 69 /** 70 * 公共分页 71 */ 72 public PageBean getPageBean(HQLHelper hh, int currentPage) { 73 int pageSize = 5; 74 int firstResult = (currentPage - 1) * pageSize; 75 String listHQL = hh.getListHQL(); 76 String countHQL = hh.getCountHQL(); 77 List<Object> args = hh.getArgs(); 78 79 Query query = this.getSession().createQuery(listHQL); 80 if(args != null && args.size() > 0){ 81 int index = 0; 82 for(Object o : args){ 83 query.setParameter(index++, o); 84 } 85 } 86 query.setFirstResult(firstResult); 87 query.setMaxResults(pageSize); 88 List recordList = query.list(); 89 90 query = this.getSession().createQuery(countHQL); 91 if(args != null && args.size() > 0){ 92 int index = 0; 93 for(Object o : args){ 94 query.setParameter(index++, o); 95 } 96 } 97 Long recordCount = (Long) query.uniqueResult(); 98 99 return new PageBean(currentPage, pageSize, recordCount.intValue(), recordList); 100 } 101 102 }
4.最后还有一个工具类:
1 package cn.itcast.oa.utils; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 辅助生成HQL语句的工具类 8 * @author zhaoqx 9 * 10 */ 11 public class HQLHelper { 12 private String fromStr;//FROM 子句 13 private String whereStr = "";//WHERE 子句 14 private String orderByStr = "";//ORDER BY 子句 15 16 private List<Object> args = new ArrayList<Object>();//封装HQL中对应的参数信息 17 18 public HQLHelper() {} 19 20 /** 21 * 通过构造方法拼接FROM 子句 22 * @param clazz 23 */ 24 public HQLHelper(Class clazz) { 25 this.fromStr = "FROM " + clazz.getSimpleName() + " o "; 26 } 27 28 /** 29 * 拼接WHERE 子句 30 * @param condition 31 * @param args 32 */ 33 public void addWhere(String condition,Object...args){//o.name = ? 34 if(this.whereStr.length()==0){ 35 //第一次拼接WHERE子句 36 this.whereStr = " WHERE " + condition; 37 }else{ 38 //不是第一次拼接WHERE子句 39 this.whereStr += " AND " + condition; 40 } 41 if(args != null && args.length > 0){ 42 //封装参数 43 for(Object o : args){ 44 this.args.add(o); 45 } 46 } 47 } 48 49 /** 50 * 拼接ORDER BY 子句 51 * @param orderBy 52 * @param asc 53 */ 54 public void addOrderBy(String orderBy , boolean asc){ 55 if(this.orderByStr.length() == 0){ 56 //第一次拼接ORDER BY 子句 57 this.orderByStr = " ORDER BY " + orderBy + (asc ? " ASC " : " DESC "); 58 }else{ 59 //不是第一次拼接ORDER BY 子句 60 this.orderByStr += ", " + orderBy + (asc ? " ASC " : " DESC "); 61 } 62 } 63 64 /** 65 * 获取查询List集合的HQL语句 66 * @return 67 */ 68 public String getListHQL(){ 69 return this.fromStr + this.whereStr + this.orderByStr; 70 } 71 72 /** 73 * 获取查询统计记录数的HQL 74 * @param args 75 */ 76 public String getCountHQL(){ 77 return "SELECT COUNT(*) " + this.fromStr + this.whereStr; 78 } 79 80 public void setArgs(List<Object> args) { 81 this.args = args; 82 } 83 84 public List<Object> getArgs() { 85 return args; 86 } 87 88 }
好了,这样一个通用的分页就完成啦。。。
以上是关于SSH2 框架下的分页的主要内容,如果未能解决你的问题,请参考以下文章