---
title: "在hibernate中使用jdbc处理复杂的sql语句 "
author: "giky2323"
date: "2007-12-23T14:26:58Z"
tags: ["Hibernate", "JDBC", "SQL", "Spring", "框架"]
slug: "hibernate-jdbc-sql"
draft: false
lastmod: "2007-12-23T14:26:58Z"
id: 150271
url: "https://giky2323.iteye.com/blog/150271"
---

想写的SQL语句是：
insert into zycbltable1(danweiid,danweimc,regedittime,cs,zmkczd,zmkcwd,qckczd,qckcwd,bylrzd,bylrwd,bylczd,bylcwd,qmkczd,qmkcwd,tjrq)select left(danweiid,15),'余干',regedittime,count(ch),sum(zmkczd),sum(zmkcwd),sum(qckczd),sum(qckcwd),sum(bylrzd),sum(bylrwd),sum(bylczd),sum(bylcwd),sum(qmkczd),sum(qmkcwd),now() from zycbltable2 where year(regedittime)=2007 and month(regedittime)=12 group by left(danweiid,15)
	      

参考文章：

在现有的spring 整和的hibernate框架中，hibernate不能处理一些复杂的带有数据库参数的SQL语句，所以采用jdbc的方法进行处理，具体处理方法如下：
 
关键是Session session = getSession(); 的获取方法参见：http://ghsea.iteye.com/category/16268?list=1 Spring整合Hibernate
 
 public List getProcontentUndergcid(Long proid)
 {   
      List procontList = null;
      Session session = getSession();
      Connection connection = session.connection();
      try{
      Statement stmt = connection.createStatement();
      ResultSet rs = stmt.executeQuery("select   sum(decimal(conitem2,10,2)) as aa ,comid from UBL_PROCONTENT where proid="+proid+" group by  comid");
      this.readDataListFromRs(rs);
       rs.close();
       session.close();
      }catch(Exception e){

      }
      return procontList;
     }

 

/**
    * 读取值对象列表
    * @param rs 结果集
    * @return 从结果集中读取的对象列表
    */
   private List readDataListFromRs(ResultSet rs) throws SQLException {
     List arr = new ArrayList();
     UblProcontent procontent = null;
     try{
       while (rs.next()) {
       procontent = new UblProcontent();
       //将字段值set到值对象中
       procontent.setConitem2(rs.getString("aa"));
       logger.info("合计数量为"+rs.getString("aa"));
       procontent.setCondemo(rs.getString("comid"));
       logger.info("编号为--"+rs.getString("comid"));
       arr.add(procontent);
       }
     }catch( Exception ex ){
        ex.printStackTrace();
     }
     return arr;
   }




修改了获得session的方法，我获得session的方法是：
Session session = getHibernateTemplate().getSessionFactory().openSession();