天天看點

資料結果封裝

通常我們需要利用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<foruminfos> 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[0];

Long times=(Long)obj[1];

Long topic=(Long)obj[2];

Long left=(Long)obj[3];

Long reply=(Long)obj[4];

Integer id=(Integer)obj[5];

//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<foruminfos> 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<foruminfos>,最後在JSP頁面中進行展示:

<table width="700" cellpadding="0" cellspacing="0"id="table">

<tr>

<th width="25%" scope="col">論壇分類</th>

<th width="25%" scope="col">文章數量</th>

<th width="25%" scope="col">留言數量</th>

<th width="25%" scope="col">回複數量</th>

</tr>

<s:iterator value="foruminfos">

<tr>

<td><a href="showforumtopic.action?cateforumid=${cateforumid}" target="_blank" rel="external nofollow" ><s:property value="name"/></a></td>//擷取此論壇類别的所有文章清單

<td><s:property value="num_topic"/></td>

<td><s:property value="num_leftwd"/></td>

<td><s:property value="num_reply"/></td>

</tr>

</s:iterator>

</table>

</div>

接着我們利用第三種方法:我們對上述表進行關聯設定,然後再利用Hibernate提供的級聯查詢進行解決。

待續。。。。。。。