spring3+hibernate3 datagrid分页的实现

1. 在controller层:返回DataGridResult


@RequestMapping(value="/list",method=RequestMethod.POST)
public @ResponseBody DataGridResult list(DataGridModel dataGridModel) throws Exception{
		
		return staffService.findPagedStaff(dataGridModel);
		
	}

2. Service层:组装dataGridModel

@Override
	public DataGridResult findPagedStaff(DataGridModel dataGridModel){
DataGridResult dataGridResult = new DataGridResult(staffDao.findTotalByPagedStaff(), 
				staffDao.findPagedStaff(dataGridModel));
		return dataGridResult;
	}

3.dao的实现:获取查询数据及其条数

   查找数据

 

@Override
	public List<HashMap<String, Object>> findPagedStaff(DataGridModel dataGridModel) {

		List<String> paramNames= new LinkedList<String>();// 存放命名参数
        List<Object> paramValues = new LinkedList<Object>();// 存放命名参数的值
		//增加列表字段,如部门、职位
		String hql = "select new map(s.id as id,s.number as number,s.name as name,s.sex as sex, s.telephone as telephone," +
                       " s.address as address,s.qq as qq,s.email as email,r.name as roleName)" +
			" from "+Staff.class.getSimpleName() + " s ," + Role.class.getSimpleName() + " r " + " where s.roleId=r.id";
                        final String tempHql = hql;
                        final DataGridModel gridModel = dataGridModel;
			final List<String> tempParamsName = paramNames;
			final List<Object> tempParamValues = paramValues;
			return hibernateTemplate.execute(
				new HibernateCallback<List<HashMap<String, Object>>>(){
					public List<HashMap<String, Object>> doInHibernate(Session session) throws HibernateException,SQLException {
						org.hibernate.Query query = session.createQuery(tempHql);
						if (tempParamsName != null&& tempParamValues.size() > 0){
						for (int i = 0; i < tempParamValues.size(); i++) {
							//参数中有数组形式的 就使用setParameterList设置参数
							if((tempParamValues.get(i)).getClass().isArray()){
								query.setParameterList(tempParamsName.get(i), (Object[])tempParamValues.get(i));
							}else{
								query.setParameter(tempParamsName.get(i),tempParamValues.get(i));}}
							}//分页的关键点,设置开始值和最大值 
							query.setMaxResults(gridModel.getRows());
							query.setFirstResult(gridModel.getFirstResult());
							List<HashMap<String, Object>> list = query.list();
							return list;
						}
					}
			);
	}

    数据条数 

@Override
	public Integer findTotalByPagedStaff() {

        List<String> paramNames= new LinkedList<String>();// 存放命名参数
        List<Object> paramValues = new LinkedList<Object>();// 存放命名参数的值

		//增加列表字段,如部门、职位 //使用count(s)函数  
		String hql = "select count(s) " +
			" from "+Staff.class.getSimpleName() + " s ," + Role.class.getSimpleName() + " r " + " where s.roleId=r.id";
			final String tempsql = hql;         
			final String[] tempParamNames = paramNames;         
			final Object[] tempParamValues = paramValues;         
			return hibernateTemplate.execute(new HibernateCallback<Integer>() { 
				public Integer doInHibernate(Session session) throws HibernateException {                 
					org.hibernate.Query query = session.createQuery(tempsql);                 
					if (tempParamNames != null && tempParamNames.length > 0) {                     
						for (int i = 0; i < tempParamNames.length; i++) {                         
							query.setParameter(tempParamNames[i],tempParamValues[i]);                     
						}                 
					}                 
					return ((Long) query.uniqueResult()).intValue();             
				}         
			});
		return count;
	}


 





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值