ssh框架 分页

一、编写Page、PageUtil

public class Page<T> {

	// 每页显示记录数
	private int pageSize;
	// 总记录数
	private int totalCount;
	// 总页数
	private int totalPage;
	// 当前页
	private int currentPage;

	private List<T> items = new LinkedList<T>();

	public Page(int pageSize, int totalCount, int totalPage, int currentPage, List<T> items) {
		super();
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		this.totalPage = totalPage;
		this.currentPage = currentPage;
		this.items = items;
	}

	public List<T> getItems() {
		return items;
	}

	public void setItems(List<T> items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

}

PageUtil.java

public class PageUtil {

	@SuppressWarnings("unchecked")
	public static Page<?> createPage(int pageSize, int currentPage,int totalCount,List<?> items) {
		pageSize = getPageSize(pageSize);
		currentPage = getCurrentPage(currentPage);
		int totalPage = getTotalPage(pageSize, totalCount);
		return new Page(pageSize,totalCount,totalPage, currentPage,items);
	}

	// 设置每页显示记录数
	public static int getPageSize(int pageSize) {
		return pageSize <= 0 ? 10 : pageSize;
	}

	// 设置当前页
	public static int getCurrentPage(int currentPage) {
		return currentPage <= 0 ? 1 : currentPage;
	}

	// 设置总页数,需要总记录数,每页显示多少
	public static int getTotalPage(int pageSize, int totalCount) {
		int totalPage = 0;
		if (totalCount % pageSize == 0) {
			totalPage = totalCount / pageSize;
		} else {
			totalPage = totalCount / pageSize + 1;
		}
		return totalPage;
	}
}

二、编写分页实现

1、BaseDaoImpl增加3个方法(获取总记录数量、获取分页数据、)

   /**
	 * @Description: 按hql条件查询记录总数
	 * @author liuh
	 */
	@SuppressWarnings({ "unchecked", "hiding" })
	public <T> T unique(final String hql, final Object... paramList) {
		T countAll = (T) getHibernateTemplate().execute(new HibernateCallback<T>() {
			public T doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				if (paramList != null) {
					for (int i = 0; i < paramList.length; i++) {
						query.setParameter(i, paramList[i]);
					}
				}
				return (T) query.uniqueResult();
			}
		});
		return countAll;
	}
    /**
	 * @Description: 按hql条件查询语句返回分页结果
	 * @author liuh
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<T> listPage(final String hql, final int currentPage2, final int pageSize2, final     Object... paramlist) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = (Query) session.createQuery(hql);
				if (paramlist != null) {
					for (int i = 0; i < paramlist.length; i++) {
						query.setParameter(i, paramlist[i]);
					}
				}
				int pageSize = PageUtil.getPageSize(pageSize2);
				int currentPage = PageUtil.getCurrentPage(currentPage2);
				currentPage = currentPage - 1;
				if (currentPage > -1 && pageSize > -1) {
					query.setMaxResults(pageSize);
					query.setFirstResult(currentPage * pageSize);
				}
				return query.list();
			}
		});
	}
   /**
	 * @Description:获取一个分页数据,包含list和totalCount
	 * @author liuh
	 */
	@SuppressWarnings("unchecked")
	public Page<T> getPageData(final String hql, final int currentPage, final int pageSize, final Object... paramlist) {
		Page<T> page = null;
		String hql2 = "select count(*) " + hql;
		Number tempCountAll = this.unique(hql2, paramlist);
		int totalCount = tempCountAll.intValue();
		List<T> list = this.listPage(hql, currentPage, pageSize, paramlist);
		page = (Page<T>) PageUtil.createPage(pageSize, currentPage, totalCount, list);
		return page;
	}

2、在业务dao类中调用BaseDaoImpl的getPageData

    public Page<Key> findPageKeyByName(String keyName, int pageSize, int currentPage) {
		return getPageData(" from Key where keyName=? ", currentPage, pageSize, keyName);
	}

3、在业务service类中增加分页接口

public Page<Key> findPageKeyName(String keyName, int pageSize, int currentPage);

4、在业务service实现类中实现分页实现

    public Page<Key> findPageKeyName(String keyName, int pageSize, int currentPage) {
		return userDaoImpl.findPageKeyByName(keyName, pageSize, currentPage);
	}

5、测试

   public void test6(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		IKeyService keyServiceImpl=(IKeyService) ctx.getBean("keyServiceImpl");
        //获取第3页数据,没有10条数据
		Page<Key> page2=keyServiceImpl.findPageKey(10, 3);
		System.out.println(page.getItems().get(0).getKeyName());
		System.out.println(page.getItems().get(0).getId());
	}

 

转载于:https://my.oschina.net/liuh1988/blog/1073063

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值