First they ignore you
then they ridicule you
then they fight you
then you win
    -- Mahatma Gandhi
Chinese => English     英文 => 中文             
随笔-219  评论-1047  文章-0  trackbacks-0
java 代码
  1. class MainController {  
  3.   def dataSource // using the datasource we define in the spring's resources.xml  
  5.   def index = {  
  6.       Sql sql = new Sql(dataSource)  
  7.       def row = sql.execute("call create_daily_hours(${new Date()+1})")  
  8.   }  
  9. }  


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">
<property name="url">
<property name="username">
<property name="password">

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.


朝花夕拾——Groovy & Grails
posted on 2007-06-04 19:39 山风小子 阅读(1733) 评论(0)  编辑  收藏 所属分类: Groovy & Grails