简体   繁体   English

在休眠3中使用存储过程

[英]using stored procedure in hibernate 3

您好,自5个月以来,我一直在使用hibernate + spring,但从未在hibernate中使用过存储过程,因此,任何人都可以告诉我如何从DB(MySQL)调用存储过程。

Hibernate defines stored procedure calls as a named query. Hibernate将存储过程调用定义为命名查询。 The docs explain how to set this up in the Hibernate config. 该文档说明了如何在Hibernate配置中进行设置

From Spring, you can call a named query using the various HibernateTemplate.findByNamedQuery(...) methods. 从Spring开始,您可以使用各种HibernateTemplate.findByNamedQuery(...)方法来调用命名查询。

Spring has an StoredProcedure class that you can extend to call stored procedures. 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) {
          }
     }
}

Since you are already using Spring with Hibernate I would suggest using Spring classes. 由于您已经将Spring与Hibernate结合使用,因此建议您使用Spring类。 You can extend the StoredProcedure class mentioned above, there are other alternatives as well. 您可以扩展上面提到的StoredProcedure类,还有其他替代方法。 If you have a basic stored procedure I would say the easiest way to call it would be to use Spring's SimpleJdbcCall class. 如果您有一个基本的存储过程,我会说最简单的调用方法是使用Spring的SimpleJdbcCall类。 The Spring documentation covers this class quite nicely with code snippets. Spring文档通过代码片段很好地涵盖了该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM