hibernate多表查询 JSTL前台显示
时间:2010-05-25 来源:beanu
先看一下数据库的截图(goodsinfo表1)

(goodscate表2)

表1中的cateid和表2中的cateId相对应。如何实现前台的如下显示呢?

现在hibernate中已经有了一个实体类goodsinfo,hql查询语句为
Query query = session.createQuery("select new Goodsinfo(a.goodsId,a.goodsName,a.price,a.unit,b.cateName) from Goodsinfo a,Goodscate b where a.cateid=b.cateId order by a.goodsId");
List all = query.list();
结果存在了List中,new Goodsinfo这句话很重要,如果没有这个前台的jstl也不会正常显示,这是我们创建一个新的Goodsinfo,Goodsinfo中要有相应的构造函数。
此时List中的每个元素都得到了正确的结果,接下来就是前台的显示。
action中已经设置为
List all=gdao.getAllInfo();
request.setAttribute("allinfo", all);
前台jsp页面
<c:forEach items="${allinfo}" var="all" >
<span>${all.goodsId}</span>
<span>${all.goodsName}</span>
<span>${all.price}</span>
<span>${all.unit}</span>
<span>${all.cateid}</span>
</c:forEach>

(goodscate表2)

表1中的cateid和表2中的cateId相对应。如何实现前台的如下显示呢?

现在hibernate中已经有了一个实体类goodsinfo,hql查询语句为
Query query = session.createQuery("select new Goodsinfo(a.goodsId,a.goodsName,a.price,a.unit,b.cateName) from Goodsinfo a,Goodscate b where a.cateid=b.cateId order by a.goodsId");
List all = query.list();
结果存在了List中,new Goodsinfo这句话很重要,如果没有这个前台的jstl也不会正常显示,这是我们创建一个新的Goodsinfo,Goodsinfo中要有相应的构造函数。
此时List中的每个元素都得到了正确的结果,接下来就是前台的显示。
action中已经设置为
List all=gdao.getAllInfo();
request.setAttribute("allinfo", all);
前台jsp页面
<c:forEach items="${allinfo}" var="all" >
<span>${all.goodsId}</span>
<span>${all.goodsName}</span>
<span>${all.price}</span>
<span>${all.unit}</span>
<span>${all.cateid}</span>
</c:forEach>
相关阅读 更多 +