最近写了一个实习管理系统,基本功能都实现了,现在想把分页加上去,不过这系统里面有很多需要分页的地方。比如管理员可以查看学生列表,校内导师和企业导师列表,学生可以查看选择校内导师和企业导师,想写一个在每个页面都能用的分页,但没有思路,要是每个页面都单独写的话肯定很麻烦
系统是mysql数据库,mvc模式,没有用框架,还没学。。
系统是mysql数据库,mvc模式,没有用框架,还没学。。
解决方案 »
- 【聊聊JAVA的新技术】
- 怎样通过hibernate的annotations生成创建表结构的sql?
- 关于jfreechart,在线等待高人指点!!!!
- java web开发中的控件动态创建
- [求助] ajax的服务器端没有返回值?
- 系统的安全问题啊,做过的人过来指点下,在线等。。。。
- XP下如何整合Tomcat5.5+SQLserver2000
- 关于struts数据存储的问题!
- 关于在JSP中使用有返回值的存储过程
- 为什么把编译好的class文件部署到tomcat 里面后会报错误404
- 页面上打开指定文件夹
- hibernate+sybase出现的错误: Neither language name in login record 'chinese' nor langu
public class Page<T> {
// -- 分页参数 --//
/**
* 当前页
*/
protected int pageNum = 1; /**
* 每页的记录条数
*/
protected int pageSize = 10;
protected int totalPages=1; protected List<Sort> orders = new ArrayList<Sort>(); protected boolean autoCount = true; // -- 返回结果 --//
protected List<T> rows = new ArrayList<T>(); /**
* 总记录数
*/
protected long total = -1; // -- 构造函数 --//
public Page() {
} public Page(int pageSize) {
this.pageSize = pageSize;
} public Page(int pageSize, int currentPage, long total) {
this.pageSize = pageSize;
this.pageNum = currentPage;
this.total = total;
} // -- 访问查询参数函数 --//
/**
* 获得当前页的页号,序号从1开始,默认为1.
*/
public int getPageNum() {
return pageNum;
} /**
* 设置当前页的页号,序号从1开始,低于1时自动调整为1.
*/
public void setPageNum(final int page) {
this.pageNum = page; if (page < 1) {
this.pageNum = 1;
}
} public Page<T> page(final int thePage) {
setPageNum(thePage);
return this;
} /**
* 获得每页的记录数量,默认为1.
*/
public int getPageSize() {
return pageSize;
} /**
* 设置每页的记录数量,低于1时自动调整为1.
*/
public void setPageSize(final int pageSize) {
this.pageSize = pageSize; if (pageSize < 1) {
this.pageSize = 1;
}
} public Page<T> pageSize(final int thePageSize) {
setPageSize(thePageSize);
return this;
} /**
* 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
*/
public int getFirst() {
return ((pageNum - 1) * pageSize);
} /**
* 获得排序方向.
*/
public Sort[] getOrders() {
Sort[] sorts = new Sort[orders.size()];
return orders.toArray(sorts);
} /**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final Sort order) {
orders.add(order); return this;
} /**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final String field) {
orders.add(Sort.add(field, Sort.OrderStyle.ASC)); return this;
} /**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final String field, final Sort.OrderStyle style) {
orders.add(Sort.add(field, style)); return this;
} /**
* 是否已设置排序字段,无默认值.
*/
public boolean isOrderBySetted() {
return orders.size() > 0;
} /**
* 查询对象时是否自动另外执行count查询获取总记录数, 默认为false.
*/
public boolean isAutoCount() {
return autoCount;
} /**
* 查询对象时是否自动另外执行count查询获取总记录数.
*/
public Page<T> setAutoCount(final boolean autoCount) {
this.autoCount = autoCount; return this;
} public Page<T> autoCount(final boolean theAutoCount) {
setAutoCount(theAutoCount);
return this;
} // -- 访问查询结果函数 --// /**
* 取得页内的记录列表.
*/
public List<T> getRows() {
return rows;
} /**
* 设置页内的记录列表.
*/
public Page<T> setRows(final List<T> rows) {
this.rows = rows; return this;
} /**
* 取得总记录数, 默认值为-1.
*/
public long getTotal() {
return total;
} /**
* 设置总记录数.
*/
public Page<T> setTotal(final long total) {
this.total = total; return this;
} /**
* 根据pageSize与totalCount计算总页数, 默认值为-1.
*/
public long getTotalPages() {
if (total < 0)
return -1; long count = total / pageSize;
if (total % pageSize > 0) {
count++;
}
return count;
} /**
* 是否还有下一页.
*/
public boolean isHasNext() {
return (pageNum + 1 <= getTotalPages());
} /**
* 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
*/
public int getNextPage() {
return isHasNext() ? pageNum + 1 : pageNum;
} /**
* 是否还有上一页.
*/
public boolean isHasPre() {
return (pageNum - 1 >= 1);
} /**
* 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
*/
public int getPrePage() {
return isHasPre() ? pageNum - 1 : pageNum;
}}分页数据的话。你可能要大修改了。把返回的集合变成Page<类型> 参数也传递这个page
把查询到的数据集合 封装到page.setRows(数据);
package common;import java.util.List;import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class Zmxpage extends HibernateDaoSupport { private int first;//从哪开始
private int pageSize;//一页存在多少数据
private String hql;//查询语句
private String hql1;
public String getHql1() {
return hql1;
} public void setHql1(String hql1) {
this.hql1 = hql1;
} /*
* Zmxpage z = new Zmxpage();
* z.getPages(1,10"from BeanName");
* List list = z.findByPage();
*/
// 分页方法
public void getPages(int currentPage,int pageSize,String hql)
{
this.first = (currentPage-1)*pageSize;
this.pageSize = pageSize;
this.hql = hql;
}
public List findByPage()
{
try {
List result = (List)getHibernateTemplate().execute(
new HibernateCallback()
{
public Object doInHibernate(Session sess)throws HibernateException
{
List tmp = sess.createQuery(hql)
.setFirstResult(first)
.setMaxResults(pageSize)
.list();
return tmp;
}
});
return result;
} catch (RuntimeException re) {
throw re;
}
}
public int gettotalPages()
{//获取总页数
try {
List list = getHibernateTemplate().find("select count(*) "+hql);
int j=0;
j = ((Long)list.get(0)).intValue();
int i = 0;
if(j%pageSize==0) i = j/pageSize;
else i = j/pageSize+1;
return i;
} catch (RuntimeException re) {
throw re;
}
}}
public String show()
{
int pagesize = 5; //分页尺度
if(currentpage==0) currentpage=1;
if(name==null) name="";
if(sex==null) sex="";
//分页显示
page.getPages(currentpage, pagesize, "from Ryb where name like '%"+name+"%' and sex like '%"+sex+"%'");
pages = page.gettotalPages();//获取总页数
list = page.findByPage();
// list = rdao.findAll();
return "show";
}
private Zmxpage page;//分页 public Zmxpage getPage() {
return page;
}
public void setPage(Zmxpage page) {
this.page = page;
}然后你别忘了注入就能用了 挺简单 看看就懂了