我正处于开发阶段,尝试使用内存数据库并在 Grails 应用程序启动期间加载一些数据。我的问题是,有没有办法编写/配置可以在启动期间执行的 SQL 插入语句。
最佳答案
您可以在 BootStrap.groovy 中执行此操作。如果为 dataSource
bean 添加依赖项注入(inject),则可以将其与 groovy.sql.Sql
实例一起使用来执行插入:
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'])
}
}
不过,假设这些表是使用域类管理的,那么使用 GORM 可能会更好。例如。运行类似 new Book(author: 'me', title: 'some title').save()
关于grails - 在 Grails 应用程序启动期间执行 SQL 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452933/