Hibernate分页

/**
  * Hibernate条件查询某些字段,根据需要可以进行分页处理
  * @param selectList 要查询的某些字段
  * @param orderParamMap   <String, Object> key标识哪些按哪些字段查询(用于拼sql),value标识这些字段的值
  * @param limitEntry 如果不为null,分页的条件  <Integer,Integer>key标识开始索引,value标识每页记录数
  * 
*/
@Override
	public List<Object> findField(List<String> selectList,
			Map<String, Object> paramMap,
			Map<String, String> orderParamMap, 
			Map.Entry<Integer, Integer> limitEntry) {
		
		//判断参数类型,如果不是指定的类型,则抛出异常
		if(paramMap != null && !LinkedHashMap.class.equals(paramMap.getClass())){
			throw new RuntimeException("参数paramMap必须是LinkedHashMap类型");
		}
		if(orderParamMap != null && !LinkedHashMap.class.equals(orderParamMap.getClass())){
			throw new RuntimeException("参数orderParamMap必须是LinkedHashMap类型");
		}
		
		String hql = "";
		if(selectList != null && selectList.size() > 0){
			hql += " select ";
			for(int i = 0; i < selectList.size(); i ++){
				if(i > 0){
					hql += " , ";
				}
				hql += " " + selectList.get(i) + " ";
			}
		}
		hql += "from " + GenericUtils.getGenericSuperClass(this.getClass()).getName() + " where 1=1 ";
		Map<String, Object> mapCondition = new LinkedHashMap<String, Object>();
		if(paramMap != null && paramMap.size() > 0){
			Iterator<Map.Entry<String, Object>> iter = paramMap.entrySet().iterator();
			while(iter.hasNext()){
				Map.Entry<String, Object> entry = iter.next();
				String key = entry.getKey();
				Object value = entry.getValue();
				Class<?> clazz = value.getClass();
				if(clazz == Integer.class || clazz == Double.class || clazz == Long.class){
					hql += " and " + key + " = :" + key + " ";
					mapCondition.put(key, value);
					continue;
				}
				if(key.endsWith("$")){
					key = key.substring(0, key.length()-1);
					hql += " and " + key  + " = :" + key + " ";
					mapCondition.put(key, value);
					continue;
				}
				hql += " and " + key + " like :" + key + " ";
				mapCondition.put(key, "%" + value.toString().trim() + "%");
			}
		}
		if(orderParamMap != null && orderParamMap.size() > 0){
			hql += " order by ";
			Iterator<Map.Entry<String, String>> iter = orderParamMap.entrySet().iterator();
			while(iter.hasNext()){
				Map.Entry<String, String> entry = iter.next();
				String key = entry.getKey();
				String value = entry.getValue();
				hql += " " + key + " " + value + " ";
			}
		}
		if(limitEntry != null){
			Integer key = limitEntry.getKey();
			Integer value = limitEntry.getValue();
			if(key != null && value != null){
				hql += " limit " + key + ", " + value + " ";
			}
		}
		Query query = getSession().createQuery(hql);
		if(mapCondition != null && mapCondition.size() > 0){
			Iterator<Map.Entry<String, Object>> iter = mapCondition.entrySet().iterator();
			while(iter.hasNext()){
				Map.Entry<String, Object> entry = iter.next();
				String key = entry.getKey();
				Object value = entry.getValue();
				Class<?> clazz = value.getClass();
				if(clazz == Integer.class){
					query.setInteger(key, (Integer)value);
					continue;
				}
				if(clazz == Double.class){
					query.setDouble(key, (Double)value);
					continue;
				}
				if(clazz == Long.class){
					query.setLong(key, (Long)value);
					continue;
				}
				query.setString(key, value.toString());
			}
		}
		//使用二级缓存的查询缓存
		query.setCacheable(true);
		return query.list();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值