Performing SQL Inserts During Grails Application Launch

I am in the development stage trying to use DB in memory and load it with some data during the launch of the Grails application. My question is, is there a way to write / configure SQL insert statements that can be executed at startup time.

+5
source share
1 answer

You can do this at BootStrap.groovy. If you add dependency injection for a dataSourcebean, you can use it with the instance groovy.sql.Sqlto insert:

import groovy.sql.Sql

class BootStrap {

   def dataSource

   def init = { servletContext ->
      def sql = new Sql(dataSource)
      sql.executeUpdate(
         'insert into some_table(foo, bar) values(?, ?)',
         ['x', 'y'])
   }
}

You will probably be better off using GORM, assuming these are tables that are managed using domain classes. For instance. run something likenew Book(author: 'me', title: 'some title').save()

+8
source

All Articles