java hibernate 调用oracle存储过程

1.hibernate 调用存储过程 各种方法

http://www.cnblogs.com/jerryxing/archive/2012/04/28/2475762.html

如果底层数据库(如Oracle)支持存储过程,也可以通过存储过程来执行批量更新。存储过程直接在数据库中运行,速度更加快。在Oracle数据库中可以定义一个名为batchUpdateStudent()的存储过程,代码如下:

create or replace procedure batchUpdateStudent(p_age in number) as
begin
update STUDENT set AGE=AGE+1 where AGE>p_age;
end;

以上存储过程有一个参数p_age,代表学生的年龄,应用程序可按照以下方式调用存储过程:

tx = session.beginTransaction();
Connection con=session.connection();

String procedure = "{call batchUpdateStudent(?) }";
CallableStatement cstmt = con.prepareCall(procedure);
cstmt.setInt(1,0); //把年龄参数设为0
cstmt.executeUpdate();
tx.commit();

在以上代码中,我用的是Hibernate的 Transaction接口来声明事务,而不是采用JDBC API来声明事务。

3.有返回值的存储过程

Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
CallableStatement cs = session.connection().prepareCall("{call Tj(?,?,?,?,?,?,?)}");//存储过程的名字,?是传入的参数
 
//设置参数值 我这里是设置了7个参数
cs.setString(1, startTime);
cs.setString(2, endTime);
cs.setString(3,"");
cs.setString(4,"5");
cs.setString(5,"60");
cs.setString(6,"240");
cs.setString(7,orderby);
 
//执行查询
ResultSet rs = cs.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
 
上面的方式是返回了一个ResultSet
第二种方法:
通过强大的createSQLQuery来实现
    1. Session session =HibernateSessionFactory.getSession();   
    2. SQLQuery query = session.createSQLQuery("{Call Tj(?)}"); //这里调用存储过程
    3. query.setString(1,"ddd");
    4. List list =query.list();  
    5. session.close(); 

-------------------------------->项目中的方法一

public void updateBySP(final String callsql, final Object... pi) {

getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {    -------->自动管理事务
    Connection connection = session.connection();
    CallableStatement cstmt = null;
    try {
     cstmt = connection.prepareCall(callsql);
     cstmt.clearParameters();
     if (pi != null) {    -------->参数判断及类型转换
      for (int i = 0; i < pi.length; i++) {
       if (pi[i] == null)
        cstmt.setString(i + 1, null);
       else {
        if (pi[i] instanceof java.util.Date) {
         cstmt.setDate(i + 1, new java.sql.Date(
           ((Date) pi[i]).getTime()));
        } else if (pi[i] instanceof java.sql.Date) {
         cstmt.setDate(i + 1, (java.sql.Date) pi[i]);
        } else {
         cstmt.setString(i + 1, pi[i].toString());
        }
       }
      }
     }
     return cstmt.executeUpdate();
    } finally {
     if (cstmt != null)
      cstmt.close();
     if (connection != null) {
      connection.close();
     }
    }
   }
  });

}

-------------------------------->项目中的方法二

public int executeBySQL(final String sql, final Object... args) {
  if (sql == null) {
   return 0;
  }

  Integer result = (Integer) getHibernateTemplate().execute(
    new HibernateCallback() {
     public Object doInHibernate(Session session)
       throws HibernateException, SQLException {
      SQLQuery sqlQuery = session.createSQLQuery(sql);
      if (CollectionUtils.notEmpty(args)) {
       for (int i = 0; i < args.length; i++) {
        sqlQuery.setParameter(i, args[i]);
       }
      }
      return sqlQuery.executeUpdate();
     }
    });

  return result;
 }

原文地址:https://www.cnblogs.com/mingtian521/p/4141288.html