2010-07-01 52 views

回答

0

Spring有一个StoredProcedure类,您可以扩展来调用存储过程。

class MyStoredProcedure extends StoredProcedure { 
    public MyStoredProcedure(DataSource ds) { 
      this.setDataSource(ds); 
      this.setSql("store_procedure_name"); 
      this.declareParameter(new SqlParameter("name", Types.VARCHAR); 
      this.compile(); 
    } 

    public void callProcedure() { 
      Map<string, String> inParams = new HashMap<String, String>(); 
      inParams.put("name", "taher"); 
      try { 
       execute(inParams); 
      } catch (DataAccessException dae) { 
      } 
    } 
} 
+1

虽然没有使用Hibernate。 – skaffman 2010-07-01 13:00:31

1

Hibernate将存储过程调用定义为命名查询。该文档在Hibernate配置中解释how to set this up

从Spring开始,您可以使用各种HibernateTemplate.findByNamedQuery(...)方法调用命名查询。

0

既然你已经在使用Spring与Hibernate,我会建议使用Spring类。你可以扩展上面提到的StoredProcedure类,还有其他的选择。如果你有一个基本的存储过程,我会说最简单的方法就是使用Spring的SimpleJdbcCall类。 Spring documentation用代码片段很好地覆盖了这个类。