在grailscmd脚本中填充数据库

在grailscmd脚本中填充数据库,grails,cmd,scripting,gorm,grails-2.5,Grails,Cmd,Scripting,Gorm,Grails 2.5,我有一个包含许多json文件的文件夹。我需要处理这些文件并将它们存储在mysql数据库中。为了做到这一点,我正在尝试创建一个grailscmd脚本,因为该项目使用的是grails2.5.6。 所以,我做的第一件事是:grails创建脚本上载json文件,然后grails创建了我的脚本,如下所示: includeTargets << grailsScript("_GrailsInit") target(uploadJson: "The description of the scrip

我有一个包含许多json文件的文件夹。我需要处理这些文件并将它们存储在mysql数据库中。为了做到这一点,我正在尝试创建一个grailscmd脚本,因为该项目使用的是grails2.5.6。 所以,我做的第一件事是:grails创建脚本上载json文件,然后grails创建了我的脚本,如下所示:

includeTargets << grailsScript("_GrailsInit")

target(uploadJson: "The description of the script goes here!") {
    doStuff()
}

target (doStuff: "The implementation task") {
}

setDefaultTarget(uploadJson)

我希望我的脚本在args中获得包含所有JSON文件的目录路径,获取每个文件并处理它,将其存储在数据库中。 在我的grails项目中,我有一些域类,我正在使用GORM检索和保存数据库中的新对象。 在grails脚本中访问域类并使用GORM方法将它们持久化到数据库中是否可行? 我已经尝试导入我的域类,但没有成功。我在Grails2.5文档中找不到任何内容。

请参阅位于的项目

脚本中的注释描述了正在发生的事情

如果克隆该repo并运行./grailsw populate db,您将看到该脚本可以正常工作

我希望这会有所帮助。

您是否考虑过将其用于此类手术?我们通常通过这个webconsole运行所有需要访问域类的操作
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)