Executing SQL inserts during Grails application startup

依然范特西╮ 提交于 2019-12-05 11:10:50

You can do this in BootStrap.groovy. If you add a dependency injection for the dataSource bean you can use it with a groovy.sql.Sql instance to do inserts:

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 would probably be better off using GORM though, assuming these are tables that are managed with domain classes. E.g. run something like new Book(author: 'me', title: 'some title').save()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!