struts+spring+hibernate实现数据库记分页显示

提供交流与学习..有不对的地方请给指出,,,本人非常感谢..呵呵....

struts+spring+hibernate实现数据库记分页显示

要分页就必须要有取数据库的起始位置和取多少记录,还有要有总记录

用spring来结合hibernate实现分页,
首先创建一个PageBean.java:

package com.binghe.spring;

public class PageBean {
private int count = 0; // 记录总数

private int pageSize = 20; // 每页显示记录数

private int pageCount = 0; // 总页数

private int page = 1; // 当前页数

private String totalCountSQL;// 得到总记录数sql语句

private String listSQL;// 得到查询记录sql语句

public int getCount() {
return count;
}
public void setCount(int count) {
if (pageSize != 0) {
pageCount = count / pageSize;
if (count % pageSize != 0) {
pageCount++;
}
}
this.count = count;
}
public String getListSQL() {
return listSQL;
}
public void setListSQL(String listSQL) {
this.listSQL = listSQL;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
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 String getTotalCountSQL() {
return totalCountSQL;
}
public void setTotalCountSQL(String totalCountSQL) {
this.totalCountSQL = totalCountSQL;
}
}
第二,创建一个接口:PaginateIntece.java

package com.binghe.spring;

import java.io.Serializable;
import java.util.List;

public interface PaginateIntece extends Serializable {
public List getList(PageBean page);

public String getToolsMenu(PageBean page);

public int getTotalCount(PageBean p, String str[], Object ob2[])
throws Exception;

public int getTotalCount(PageBean page) throws Exception;

public List getList(PageBean page, String str[], Object ob2[])
throws Exception;
}
第三,创建一个继承spring 的org.springframework.orm.hibernate3.support.HibernateDaoSupport的类Paginate.java,原码如下

package com.binghe.spring;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class Paginate extends HibernateDaoSupport implements PaginateIntece {

/**
* 显示用的菜单
*/
public String getToolsMenu(PageBean p) {
StringBuffer str = new StringBuffer("");
int next, prev;
prev = p.getPage() - 1;
next = p.getPage() + 1;

if (p.getPage() > 1) {
str
.append("首页 ");
} else {
str.append("首页 ");
}
if (p.getPage() > 1) {
str.append("上页 ");
} else {
str.append("上页 ");
}
if (p.getPage() < p.getPageCount()) {
str.append("下页 ");
} else {
str.append("下页 ");
}
if (p.getPageCount() > 1 && p.getPage() != p.getPageCount()) {
str.append("末页 ");
} else {
str.append("末页 ");
}
str.append(" 共" + p.getCount() + "条记录");
str
.append(" 每页"); if (p.getPageSize() == 3) { str.append(" 3 "); } else { str.append(" 3 "); } if (p.getPageSize() == 10) { str.append(" 10 "); } else { str.append(" 10 "); } if (p.getPageSize() == 20) { str.append(" 20 "); } else { str.append(" 20 "); } if (p.getPageSize() == 50) { str.append(" 50 "); } else { str.append(" 50 "); } if (p.getPageSize() == 100) { str.append(" 100 "); } else { str.append(" 100 "); } str.append(""); str.append("条 分" + p.getPageCount() + "页显示 转到"); str .append(""); for (int i = 1; i < p.getPageCount() + 1; i++) { if (i == p.getPage()) { str.append(" " + i + " "); } else { str.append(" " + i + " "); } } str.append("页");
str.append(" ");
str.append(" ");
return str.toString();
}

public int getTotalCount(PageBean p) throws Exception {

List list = getHibernateTemplate().find(p.getTotalCountSQL());
int count = 0;
if (list.size() > 0) {
count = ((Integer) list.get(0)).intValue();
}
return count;
}

public List getList(PageBean p) {
Session session = this.getSession();
Query q = session.createQuery(p.getListSQL());
q.setFirstResult((p.getPage() - 1) * p.getPageSize());
q.setMaxResults(p.getPageSize());
return q.list();
}

public List getList(PageBean p, String str[], Object ob2[]) {
Session session = this.getSession();
Query q = session.createQuery(p.getListSQL());
for (int i = 0; i < str.length; i++) {
q.setParameter(str, ob2);
}
q.setFirstResult((p.getPage() - 1) * p.getPageSize());
q.setMaxResults(p.getPageSize());
return q.list();
}

public int getTotalCount(PageBean p, String str[], Object ob2[])
throws Exception {

List list = getHibernateTemplate().findByNamedParam(
p.getTotalCountSQL(), str, ob2);
int count = 0;
if (list.size() > 0) {
count = ((Integer) list.get(0)).intValue();
}
return count;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值