继续之前那个复习,继续那3张表,在此前已经可以联级插入数据,现在来联级检索出3张表的数据,假设要求查出名字为“lzj"的学生在3张表里的所有记录。使用inner join操作。
TstudentDAO:
public List findall(String name){
Session session =this.getSession();
Transaction tx = null;
Query query = session.createQuery("select t.name,t.email,s.name,tt.times from TStudent t join t.TSubjectses s join t.TTests tt where t.name=:name");
query.setString("name", name);
List result = query.list();
List templist = new ArrayList();
for (Iterator it = result.iterator();it.hasNext();){
Object[] row=(Object[])it.next();
Map mm = new HashMap();
mm.put("name", (String)row[0]);
mm.put("email", (String)row[1]);
mm.put("sname", (String)row[2]);
mm.put("times", (Integer)row[3]);
templist.add(mm);
}
return templist;
}
action里只需要一句话:
List results = tstudentDAO.findall("lzj");
并将结果返回给struts的jsp页面:
if(results!=null){
HttpSession session=request.getSession();
session.setAttribute("results",results);
return mapping.findForward("ok");
jsp页面接受,使用struts标签:
<logic:present name="results">
<table border="1">
<logic:iterate id="element" name="results">
<tr>
<td width="100"><bean:write name="element" property="name"/></td>
<td width="100"><bean:write name="element" property="email"/></td>
<td width="100"><bean:write name="element" property="sname"/></td>
<td width="100"><bean:write name="element" property="times"/></td>
<td id="result"></td>
</tr>
</logic:iterate>
</logic:present>
posted on 2008-06-26 23:10
lzj520 阅读(321)
评论(0) 编辑 收藏 所属分类:
个人学习日记 、
Hibernate