1

如何防止在 grails 应用程序中引导数据。当我们像这样配置我们的 DataSource.groovy

 development {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/test"
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        dialect = org.hibernate.dialect.MySQL5InnoDBDialect
        username = "root"
        password = "root"
    }

}

BootStrap.groovy

class BootStrap {
def bootstrapService
def grailsApplication
def init = { servletContext ->
    switch (Environment.getCurrent().name) {
        case "dev":
            bootstrapService.bootstrapDummyData()
            break;
        case "test":
            bootstrapService.bootstrapDummyData()
            break;
    }
}
def destroy = {
}

bootstrapService.bootstrapDummyData()当我将数据源配置为更新模式时,我不想被调用。IEdbCreate = "update"

4

2 回答 2

2
 class BootStrap {
 def bootstrapService
 def grailsApplication
 def init = { servletContext -> 
 if(grailsApplication.config.dataSource.dbCreate == "create-drop"){
    switch (Environment.getCurrent().name) {
        case "dev":
            bootstrapService.bootstrapDummyData()
            break;
        case "test":
            bootstrapService.bootstrapDummyData()
            break;
     }
   }
}
于 2014-04-02T06:11:15.513 回答
0
def init(){

    if(User.list().size()==0)
       objectCreation()
}

def objectCreation(){

def userObject1 = new User()
def userObject2 = new User()


}

例如上面的代码。如果用户是在 bootstarp 中创建的。首先检查用户是否已经创建。如果不创建对象。

于 2014-03-27T09:27:07.097 回答