如何使用参数调用我的存储过程在Grails中形成域类

时间:2014-07-03 06:21:44

标签: grails stored-procedures

需要有关如何从Grails中的域类调用我的存储过程的示例。

2 个答案:

答案 0 :(得分:1)

您可以Groovy SQL使用存储过程支持。

在您的域中定义dataSource(它的spring bean将被注入) 使用dataSource创建Sql对象并使用sql执行存储过程。

class myDomain{
    def dataSource

  myMethod{
        Sql sql = new Sql(dataSource)
        sql.call("{? = call FullName(?)}", [Sql.VARCHAR, 'Sam']) { name ->
          ....
        }
 }
 }

答案 1 :(得分:0)

Grails只是一个弹簧应用程序,所以你可以在你的grails应用程序中使用任何spring api。在这种情况下,我们可以使用JdbcTemplate类来调用存储过程。

以下是一个示例代码,您可以在从This post获取的服务类中使用JdbcTemplate。

class StoredProcedureRunnerService {
 def dataSource
 boolean transactional = true

 // automatically set up by name (Spring injected) on startup
 def sumBookSalesByBookIdWithJdbcTemplate(def bookId) {
     def template = new JdbcTemplate(dataSource)
        template.queryForInt("select salesByBookId(${bookId})")
     }
  }