前两天 做项目 需要 拿到hibernate 读取的自定义列 网上找了个方法 供大家使用哈
什么也不说了 先上代码吧
public Map<Long, Long> getUserNoticesByUserId(Long userId) throws Exception { Map<Long, Long> map = new HashMap<Long, Long>(); StringBuffer hql = new StringBuffer( "SELECT count(*),t.type FROM UserNotice t WHERE t.state = 1L"); if (ObjectUtils.isNotEmpty(userId)) { hql.append(" AND t.userId =:userId"); } hql.append(" group by t.type "); Session session = getSession();//这里是框架中使用的 大家可以通过 getHibernateTemplate 获取session Query query = session.createQuery(hql.toString());//创建query对象 query是 hibernate的类 if (ObjectUtils.isNotEmpty(userId)) { query.setParameter("userId", userId);//设置参数 } Iterator it = query.list().iterator();//输出迭代器 while (it.hasNext()) {//循环迭代器 Object[] obj = (Object[]) it.next();//在这里 会返回一个 object的数组的迭代器 在里面找到对应的列进行 数据的读取 map.put((Long) obj[1], (Long) obj[0]); } return map; }