zoukankan      html  css  js  c++  java
  • 如何使用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
      }
      });
     }

  • 相关阅读:
    需求的陷阱
    VS2008 NumericUpDown控件 内容全选
    KeyPress 事件中 keycode对应的按键
    C#发送邮件
    Stream 和Byte[] 之间的转换
    SQL ISNULL() 函数
    修改struts2的.action后缀名
    #pragma data_seg
    VBA中Option的四种用法
    SetWindowsHookEx
  • 原文地址:https://www.cnblogs.com/xiyuanbaiyun/p/2244196.html
Copyright © 2011-2022 走看看