在特殊情況下,grails應用需要調用數(shù)據(jù)庫的存儲過程,這在grails的官方文檔里邊好像沒有提到過,在james的blog里介紹如何解決這個問題。
代碼轉貼如下
Java 代碼
- class MainController {
-
- def dataSource // using the datasource we define in the spring's resources.xml
-
- def index = {
- Sql sql = new Sql(dataSource)
- def row = sql.execute("call create_daily_hours(${new Date()+1})")
- }
- }
需要說明的一些是:grails本身沒有提供訪問存儲過程的便捷方法,而groovy的GSQL提供了,因此grails可以直接拿過來用了,當然也可以用spring的JdbcTemplate。
希望對grails用戶有點用。
原文地址:http://grails.group.javaeye.com/group/blog/86666
My experience with grails is getting richer the longer I use it for web application developing. It's very nice that grails is built on top of spring framework which we can take advantage of. I am not a spring user before but with a help from the nice people at the grails forum I was able to achieve what I want to do.
Calling a stored procedure from a MySQL database or any other database is simple. First we need a datasource which spring could provide for us. I have the following code place in the resources.xml found in the spring folder in your grails folder.
<bean id="dataSource" class=" org.apache.commons.dbcp.BasicDataSource ">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
I use connection pooling for better performance. In my controller here is how I use the datasource to call a store procedure.
class MainController {
def dataSource // using the datasource we define in the spring's resources.xml
def index = {
Sql sql = new Sql(dataSource)
def row = sql.execute("call create_daily_hours(${new Date()+1})")
}
}
That's it! Notice that I am using Groovy SQL instead of Spring JDBCTemplate. It's a lot more friendlier for a beginner.
Grails really makes everything easy here and provides a lot of flexibility thanks to it's nice integration with spring. From here everything is possible.
原文地址:http://james-says.blogspot.com/2007/03/grails-little-of-spring-framework.html
posted on 2008-07-18 09:30
周銳 閱讀(590)
評論(0) 編輯 收藏 所屬分類:
Groovy&Grails