ITeye博客

数据结果封装

jasstion · 2009-08-01 12:36 · Structs · Markdown

通常我们需要利用Action返回用户需要的一些信息,当然一些信息可能很简单,但一些信息则会很复杂;现在以一个具体事例来探讨一些复杂信息的封装。 这里所说的封装是针对于Struts标签,是为了Action返回的数据能够更好的,更方便的在用户端得到展示;比如说list,这就是个很好的信息表现方式;因为很多数据标签都为其提供了很好的支持;下面对Action中得到的复杂原始信息封装成最终的List对象,便于在JSP中利用标签进行显示。 我想实现这样一个功能:用户单击相应的链接可以得到这样一个列表:论坛类别,此类别文章数目,所有文章留言数目以及所有文章那个回复数目。 涉及到的数据表有四个:cate_forumup,cate_forum,topic_blog,leftwd,reply_leftwd;它们之间的关系是一次一对多的关系;但reply_leftwd与topic_blog是一对多的关系。 利用Hibernate数据持久化框架进行数据业务层的交互,不过为缩短篇幅直接将数据业务层代码写在Action中。 第一种方法中利用最原始的SQL语句进行查询,大家看过之后就知道原始SQL居于在某些情况下的优势啦; public String showforum()throws Exception{ log.info("begain!"); List list=new ArrayList(); List qq; HttpServletRequest re=ServletActionContext.getRequest(); int cateforumupid=Integer.parseInt(re.getParameter("cateforumupid")); Session session=new BaseHibernateDAO().getSession(); Transaction tx=null; try{ SQLQuery q1=session.createSQLQuery("select b.name,sum(distinct a.times) as times,count(distinct a.ID) as topic ,count(distinct l.ID) as le,count(distinct r.ID) as r,b.ID as id from cate_forumup c left outer join cate_forum b on b.cate_forumup_ID=c.ID left outer join topic_blog a on b.ID=a.cate_forum_ID left outer join leftwd l on a.ID=l.topic_blog_ID left outer join reply_leftwd r on a.ID=r.topic_blog_ID where c.ID=1 group by b.name"); //q1.setInteger("cateforumid",1); q1.addScalar("name",Hibernate.STRING); q1.addScalar("times",Hibernate.LONG); q1.addScalar("topic",Hibernate.LONG); q1.addScalar("le",Hibernate.LONG); q1.addScalar("r",Hibernate.LONG); q1.addScalar("id",Hibernate.INTEGER); Iterator iter1=q1.list().iterator(); log.info(q1.list().size()); while(iter1.hasNext()){foruminfos inf=new foruminfos(); Object obj=(Object)iter1.next(); String name=(String)obj; Long times=(Long)obj; Long topic=(Long)obj; Long left=(Long)obj; Long reply=(Long)obj; Integer id=(Integer)obj; //log.info(name); //log.info(times); //log.info(topic); //log.info(left); //log.info(reply); inf.setName(name); inf.setNum_leftwd(left); inf.setNum_reply(reply); inf.setNum_topic(topic); inf.setNum_times(times); inf.setCateforumid(id); list.add(inf);}

		}
	catch(Exception e){}
	finally{
		session.close();
	}
	setForuminfos(list);
	return "main_forum.jsp";
}

大家可以看到利用原始语句,完成上述目的只需一个语句。 第二种方法中仅仅利用HQL查询技术。 实现上述功能的Action中的showforum()方法如下: public String showforum()throws Exception{ log.info("begain!"); List list=new ArrayList(); List qq; int reply=0; int left=0; HttpServletRequest re=ServletActionContext.getRequest(); int cateforumupid=Integer.parseInt(re.getParameter("cateforumupid")); Session session=new BaseHibernateDAO().getSession(); Transaction tx=null; try{ Query q=session.createQuery("from CateForum as a where a.cateForumupId like:cateforumupid");// q.setInteger("cateforumupid",cateforumupid); qq=q.list(); Iterator iter=qq.iterator(); while(iter.hasNext()){ foruminfos fo=new foruminfos(); CateForum up=(CateForum)iter.next(); fo.setName(up.getName()); fo.setCateforumid(up.getId()); Query q1=session.createQuery("from TopicBlog as a where a.cateForumId like:cateforumid"); q1.setInteger("cateforumid",up.getId()); Iterator iter1=q1.list().iterator(); fo.setNum_topic(q1.list().size()); while(iter1.hasNext()){TopicBlog topic1=(TopicBlog)iter1.next(); log.info("I am here"); Query q4=session.createQuery("from Leftwd as a where a.topicBlogId like:id"); q4.setInteger("id",topic1.getId()); List list1=q4.list(); left+=list1.size(); Query q5=session.createQuery("from ReplyLeftwd as a where a.topicBlogId like:id"); q5.setInteger("id",topic1.getId()); List list2=q5.list(); reply+=list2.size(); // q1.setInteger("cateforumid",topic1.getId()); } fo.setNum_leftwd(left); fo.setNum_leftwd(reply); list.add(fo); log.info(fo.name); log.info(fo.num_leftwd); log.info(fo.num_reply); log.info(fo.num_topic); log.info(fo.cateforumid); }

		}
	catch(Exception e){}
	finally{
		session.close();
	}
	setForuminfos(list);
	return "main_forum.jsp";
}

在上述的方法中,我把得到的信息组装到对象foruminfos中,然后返回这样一个数据类型:List,最后在JSP页面中进行展示:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。