0
我有一堆數據存在於一個單獨的文件中,並試圖找出如何在grails應用程序啓動時自動將這些數據加載到數據庫中。如何從JSON外部文件加載或引導數據到Grails中?
我有一堆數據存在於一個單獨的文件中,並試圖找出如何在grails應用程序啓動時自動將這些數據加載到數據庫中。如何從JSON外部文件加載或引導數據到Grails中?
我把json文件放入grails-app/conf/resources
並編輯爲Bootstrap.groovy
。
在文件的頂部,我注入Grails應用程序:
class BootStrap {
def grailsApplication
...
,後來我加載的數據:
if (!Cars.count()) {
// add cars
def filePath = "resources/carsData.json"
def text = grailsApplication.getParentContext().getResource("classpath:$filePath").getInputStream().getText()
def json = JSON.parse(text)
for (carData in json) {
def c = new Cars(
name: carData ["name"],
year: carData ["year"]
).save(failOnError: true);
}
}
我一直在試圖通過解組而不是要做到這一點這種手動方式。任何想法如何做到這一點? –