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;
} }

好了,这样一个通用的分页就完成啦。。。

SSH2 框架下的分页的更多相关文章

  1. laravel 框架 下拉分页

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  2. 微擎框架下拉分页(使用js模板引擎)

    1.需要分页的页面,引入一下文件 <script language="javascript" src="\addons\{$_GPC['m']}\template\ ...

  3. SpringMVC框架下实现分页功能

    1.创建实体类Page.java @Entity public class Page { private int totalRecord;// 表示查询后一共得到多少条结果记录 private int ...

  4. CI框架下 ajax分页

    用做于商品详情页的评论展示: html: <script> var commodityid=<?php echo $info['commodity_id'] ?>; var u ...

  5. think php 框架下拉分页

    //以对象的形式获取数据库$data变量的信息,将lastPage()传输至页面 $lastpage = $data->lastPage(); $this->assign('lastpag ...

  6. SSH2框架搭建 和 配置文件详解

    -----------补充说明----------- 文章中所列出的struts2的2.2jar包已经不是最新的了,这个版本有严重漏洞, 现在最新版本为2.3.15,所以.你懂的http://stru ...

  7. 第三百一十四节,Django框架,自定义分页

    第三百一十四节,Django框架,自定义分页 自定义分页模块 #!/usr/bin/env python #coding:utf-8 from django.utils.safestring impo ...

  8. Django框架 之 Pagination分页实现

    Django框架 之 Pagination分页实现 浏览目录 自定义分页 Django内置分页 一.自定义分页 1.基础版自定义分页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  9. phpstrom的xdebug开启和yii2下的分页的链接

    phpstrom的xdebug开启 1.修改php.ini文件(修改完重启apaceh) xdebug.remote_enable = onxdebug.idekey= PHPSTROM [注意:远程 ...

随机推荐

  1. linux常用经典命令

    1.查看cpu # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数   # 查看物理CPU个数 #物理cpu个数 ...

  2. node + nginx + mongo搭建负载均衡

    基于node和nignx和mongo搭建负载均衡 nginx配置: upstream back {                                                  # ...

  3. Windows Services

    1.本机服务查看:services.msc /s2.服务手动安装(使用sc.exe):sc create MemoryStatus binpath= c:\MyServices\MemoryStatu ...

  4. mac 启动 docker daemon

    我是用virtualbox安装的. 有一个小问题就是启动docker服务时会检查boot2docker是不是最新的. 由于github被封了,所以只能手动下 https://github.com/bo ...

  5. 《30天自制操作系统》16_day_学习笔记

    harib13a: 今天我们要继续折腾多任务,任务的高效管理是操作系统的一个重要的任务.在今天,我们将为系统创建更加完善的任务管理系统,其中包括优先级,任务等级等. 1.任务管理结构体 #define ...

  6. 获取设置唯一的UDID的值

    http://blog.sina.com.cn/s/blog_5971cdd00102vqgy.html ---方法 http://www.jianshu.com/p/a7a4a14c8030  -- ...

  7. ubuntu15.04 安装搜狗输入法

    首先:打开 系统设置->软件和更新,添加以下源, deb http://archive.ubuntukylin.com:10006/ubuntukylin trusty main 然后 sudo ...

  8. SSH三大框架的工作原理及流程

    Hibernate工作原理及为什么要用? 原理:1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.x ...

  9. .NET学习记录3

    一.ASP.NET ISAPI与IIS IIS如何监听来自外界的http request ,如何根据ISAPI Extension Maping 将对于不同的resource的请求分发给不同的ISAP ...

  10. java中的乱码问题

    1如果使用的tomcat服务器,在server.xml中Connector 标签后加 URIEncoding="UTF-8": 2使用web过滤器: (1).新建一个SetChar ...