如何在action类中使用Hibernate进行指定字段的查询
小弟初学,弄了一个信息发布系统,在前台使用Struts通过文章的类型列表循环出每一列表中的所有文章,但是在infoService中写一个getAllTypes()的函数,之前的函数都是通过find(hsql)弄得,这个还真不怎呢熟,求指教,部分代码如下public class InfoDAO extends HibernateDaoSupport implements IInfoDAO{
/**
* 默认构造函数,在使用Spring和Hibernate的环境中一般需要(反射)
*/
public InfoDAO(){
}
public Information getInfoByID(String id){
return (Information) this.getHibernateTemplate().get(Information.class, id);
}
public List<Information> getInfoByType(String type){
String hsql="from Information where type= '"+type+"'";
return this.getHibernateTemplate().find(hsql);
}
public List<Information> getAllInfo(){
String hsql="from Information";
return this.getHibernateTemplate().find(hsql);
}
public List<String> getAllTypes(){
//请问这里应该怎么写
}
}
--------------------编程问答-------------------- 没有人回答我吗、、、、 --------------------编程问答-------------------- 呼叫版主 --------------------编程问答--------------------
string hql="select type from Information";
就这句就行
hibernate给你返回的就是一个type的数组了 --------------------编程问答-------------------- 首先获取所有类型,得到一个类型列表,然后循环便利这个列表,在循环中调用getInfoByType,获取每个类型下的文章列表,数据结构可以采用HashMap来组装,伪代码如下:
HashMap <String,ArrayList<String>> result = new HashMap <String,ArrayList<String>>();
List alltypels = infoDAO.getAllTypes();
for(String type:alltypels){
List infosInType = infoDAO.getInfoByType(type);
result.add(type,infosInType);
}
补充:Java , Java EE