我是 Groovy & Grails 的新手。我想提交解析 CSV 文件并导出到 MySQL 数据库的几个表中。 我看过一些编码,但作为新手,这让我感到困惑。 那么任何人都可以帮助我理解简单的 csv 文件解析和导出到 MySQL 数据库。
谢谢 苏努
最佳答案
Grails 是一个在您的应用程序启动时运行的 Bootstrap 。它很漂亮;您可以将其配置为在不同的环境中执行不同的操作。
一种方法是在 Bootstrap 中执行以下操作:
1) 读取 csv 文件,边读边创建域对象。
2)对于每一个域对象,检查它是否存在,如果不存在则domainObject.save()
就是这样。
对于代码,比如
new File(filePath).splitEachLine(',') {fields ->
def domainObject = new YouDomainObject(
id: fields[0].trim(),
name: fields[1].trim()
)
if (domainObject.hasErrors() || domainObject.save(flush: true) == null) {
log.error("Could not import domainObject ${domainObject.errors}")
}
log.debug("Importing domainObject ${domainObject.toString()}")
}
关于mysql - 解析 CSV 并导出到 Grails 中的 Mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4784226/