情况一:
List ha = new ArrayList();
ha=session.createQuery("SELECT test2.id,test2.name FROM Test2 as test2").list();
//ha = session.createCriteria(Test2.class).list();
for (Iterator it = ha.iterator(); it.hasNext();) {
Object[] test2 = (Object[]) it.next();
System.out.print(test2[0]);
System.out.println(":"+test2[1]);
}
情况一为了也可以动态的在hql语句中构建一个新的对象,定义相应的set,get方法,然后就可以像第三种情况一样使用了,这样便于从action到jsp页面去显示:参照
《
jsp通过logic:iterate或html:optionsCollection循环显示action中传过来的对象List》
情况二:
List ha = new ArrayList();
ha=session.createQuery("SELECT test2.name FROM Test2 as test2").list();
//ha = session.createCriteria(Test2.class).list();
for (Iterator it = ha.iterator(); it.hasNext();) {
System.out.println(it.next());
}
情况三:
List ha = new ArrayList();
ha=session.createQuery("SELECT test2 FROM Test2 as test2").list();
//ha = session.createCriteria(Test2.class).list();
for (Iterator it = ha.iterator(); it.hasNext();) {
Test2 test2=(Test2)it.next();
System.out.println(test2.getName());
}