请参阅https://github.com/jeffbrown/federicobaioccoscript上的项目。
脚本中的注释描述了正在发生的事情。
https://github.com/jeffbrown/federicobaioccoscript/blob/977df5aedff04cec47d8c25900b4048cf1e12fe8/scripts/PopulateDb.groovy
includeTargets << grailsScript('_GrailsBootstrap')
target(populateDb: "Target demonstrates one approach to using domain classes in a script") {
depends classpath, bootstrap
// load the Person class
def personClass = classLoader.loadClass('federicobaioccoscript.Person')
// the question is about using domain classes in a script, not
// about parsing JSON files so here the peopleData is hardcoded instead
// of complicating the example with file i/o.
def peopleData = []
peopleData << [firstName: 'Geddy', lastName: 'Lee']
peopleData << [firstName: 'Neil', lastName: 'Peart']
peopleData << [firstName: 'Alex', lastName: 'Lifeson']
// create and persist instances of the Person class
personClass.withNewSession {
peopleData.each { personData ->
// create an instance of the Person class
def person = personClass.newInstance personData
// save the instance to the database
person.save()
}
}
// this is only here to demonstrate that the data
// really is in the database...
personClass.withNewSession {
List people = personClass.list()
println people
}
}
setDefaultTarget(populateDb)
如果您克隆该存储库并运行,./grailsw populate-db
您将看到该脚本有效。
我希望这会有所帮助。