薄雾倾城

导航

如何使用spring中hibernate返回获取list集合

举例如下:

public List<Invoice> queryInvoice(final Condition condition) {
  return (List<InvoiceResult>) getHibernateTemplate().execute(new HibernateCallback() {
     public Object doInHibernate(Session s) throws HibernateException, SQLException {

  //创建sql语句
        StringBuilder sql = new StringBuilder("select subcompany,invoiceno,amount from mm_invoice_td iv where 1 = 1 ");
        sql.append(" and iv.subcompany = :subcompany");
        if (StringUtils.hasText(condition.getInvoiceno())) {
         sql.append(" and iv.invoiceno =:invoiceno");
       }
        if (null !=condition.getAmountfrom()) {
         sql.append(" and iv.amount>= :amountfrom");
       }
      if (null != condition.getAmountto()) {
        sql.append(" and iv.amount<= :amountto");
       }
    //创建Query
      Query query = s.createSQLQuery(sql.toString())
         .addScalar("subcompany", Hibernate.STRING)//设置返回字段的类型
         .addScalar("invoiceno", Hibernate.STRING)
         .addScalar("amount", Hibernate.STRING)
           .setResultTransformer(Transformers.aliasToBean(InvoiceResult.class))//返回的泛型类
           .setString("subcompany", condition.getSubcompany())//设置条件
          

      if (StringUtils.hasText(condition.getInvoiceno())) {
        query.setString("invoiceno", condition.getInvoiceno());
      }
     if (null !=condition.getAmountfrom()) {
        query.setDouble("amountfrom", condition.getAmountfrom());
      }
      if (null != condition.getAmountto()) {
        query.setDouble("amountto", condition.getAmountto());
      }
     return query.list();//返回list
  }
  });
 }

posted on 2011-11-10 11:07  若菲  阅读(1055)  评论(0编辑  收藏  举报