hql中in关键字的用法

hql:

  from " + FoodComment.class.getName() + " f where f.id in :groupIds" 

封装的方法:

public <T> List<T> findListByHql2(final String hql, final Map<String, Object> nameParams, final Object... numParams) {
        return hibernateTemplate.execute(new HibernateCallback<List<T>>() {
            @SuppressWarnings("unchecked")
            @Override
            public List<T> doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(hql);
                for (int i = 0; i < numParams.length; ++i) {

                    //setParameter() 第二个参数接受对象
                    query.setParameter(i, numParams[i]);
                }  

                Set<String> keySet = nameParams.keySet();
                for(String key : keySet) {
                    Object obj = nameParams.get(key);
                    if(obj instanceof Collection<?>) {
                       //setParameterList() 第二个参数接受集合
                        query.setParameterList(key, (Collection<?>) obj);
                    } else if(obj instanceof Object[]) {
                        query.setParameterList(key, (Object[])obj);
                    } else {
                       //setParameter() 第二个参数接受对象
                        query.setParameter(key, obj);
                    }
                }
                return query.list();
            }
        });
    }
上一篇:严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response


下一篇:Copy-On-Write容器