Hibernate执行原生sql,list转换问题

通过Hibernate执行原生sql。把List转换成List
	/**
	 * 通过关键词查询标王信息
	 * @author zhaodong.wang
	 * findBidKingByKeyWord(这里用一句话描述这个方法的作用)
	 * TODO(这里描述这个方法适用条件 – 可选)
	 */
	private List findBidKingInfoByKeyWord(String keyWord){
		final String sql = "select bid.key_word,u.company_id from cm_bid_king bid left join t_reg_user u on(bid.login_name=u.login_name)";
		return executeSQL(sql);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List executeSQL(final String sql) {
		  return (List) dao.getHibertemplate().execute(new HibernateCallback() {
	            @Override
	            public List doInHibernate(Session session)
	                throws HibernateException, SQLException {
	                SQLQuery query = session.createSQLQuery(sql);
	                query.setResultTransformer(
	            			new ResultTransformer() {
	        					private static final long serialVersionUID = 7178852496845637376L;
	        					@Override
	        					public Object transformTuple(Object[] values, String[] aliases) {
	        						if(ObjectUtils.isEmpty(aliases)) {
	        							return null;
	        						}
	        						Map rowMap = new StringKeyCaseInsensitiveMap();
	        						
	        						for(int i = 0 ; i < aliases.length; i ++) {
	        							rowMap.put(aliases[i], values[i]);
	        						}
	        						return rowMap;
	        					}
	        					
	        					@SuppressWarnings("rawtypes")
	        					@Override
	        					public List transformList(List collection) {
	        						return collection;
	        					}
	            			});
	        			return Collections.unmodifiableList(query.list());
	            }
	        });
	}
以下是内部类,提供键值对转换
private static class StringKeyCaseInsensitiveMap extends HashMap {


		private static final long serialVersionUID = 1L;


		@SuppressWarnings("unchecked")
		public V put(K key, V value) {
			if(key instanceof String) {
				super.put((K) (((String)key).toLowerCase()), value);
			} else {
				super.put(key, value);
			}
			return value;
		};
		
		@SuppressWarnings("unchecked")
		@Override
		public V get(Object key) {
			if(key instanceof String) {
				return super.get((K) (((String)key).toLowerCase()));
			}
			return super.get(key);
		}
	}

你可能感兴趣的:(工具类相关)