Spring MVC 如何显示自定义的sql语句的query.getResultList()的数据?

在vs的codeBehind中,我可以这样显示数据:
  string strSql = "select userId,count(*) as loginTimes
         from loginLog where loginDate > '2011-01-01';        //构建sql语句
  DataSet ds = bll.getList(strSql);                           //调用业务层方法,返回数据列表到数据集
  rptList.dataSource = ds;
  rptList.dataBind();                                         //将数据列表绑定到repeater控件中

然后在前台中,使用
<table width="100%" height="97" border="0" cellpadding="0" cellspacing="0">
  <asp:Repeater id="rptList" runat="server">
    <ItemTemplate>
      <tr>
        <td width="30%"> <%# DataBinder.Eval(Container.DataItem,"userId") %></td>
        <td width="70%"> <%# DataBinder.Eval(Container.DataItem,"loginTimes") %> </td>
      </tr>
    </ItemTemplate>
  </asp:Repeater>
</table>
循环将统计的各位用户上网次数列表显示出来;

我的问题是,springMVC中,如果业务层从数据库列表返回的是一个类的对象列表,也可以用forEach循环显示;
如果返回的是一个sql语句写的统计列表,就像上面的sql语句那样,而系统中没有相应的类和它对应,如何将结果显示到页面上去?

 可以参考这个:主题-Hibernate查询详解, http://www.iteye.com/topic/824531
也可以参考这个,我认为它比较满意地回答了我的问题: 标题:使用 JPQL 和原生 SQL 查询 JPA 实体地址,http://www.oracle.com/technetwork/cn/articles/vasiliev-jpql-085775-zhs.html?ssSourceSiteId=otnen 这篇文章写的非常好,深入、细致而明明白白地说明了问题的产生与解决的对策:
package jpqlexample.servlets;
...

class LineItemSum {
       private Double price;
       private Integer quantity;
       private Double rslt;
       public LineItemSum (Double price, Integer quantity){
             this.rslt = quantity*price;
       }
       public Double getRslt () {
            return this.rslt;
       }
       public void setRslt (Double rslt) {
            this.rslt = rslt;
       }
    }

public class JpqlJoinsServlet extends HttpServlet {
    ...

    public void doGet(
        ...
        List<LineItemSum> arr = (List<LineItemSum>)em.createQuery
                    ("SELECT NEW jpqlexample.servlets.LineItemSum(p.price, l.quantity) FROM PurchaseOrder o 
                            JOIN o.orderLineItems l JOIN l.product p JOIN p.supplier s WHERE s.sup_name = 'Tortuga Trading'")
                              .getResultList(); 
        Iterator i = arr.iterator();
        LineItemSum lineItemSum;
        Double sum = 0.0;
        while (i.hasNext()) {
            lineItemSum = (LineItemSum) i.next();
            sum = sum + lineItemSum.getRslt();
        }
        out.println("The total cost of the ordered products supplied by Tortuga Trading: "+ sum + "<br/>");
    }
}
 

猜你喜欢

转载自xiegs2007.iteye.com/blog/1070734