別のファイルに存在する大量のデータがあり、grails アプリケーションの起動時にこのデータをデータベースに自動的にロードする方法を見つけようとしています。
2336 次
1 に答える
4
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);
}
}
于 2013-07-11T20:44:30.990 に答える