解析CSV并导出到Grails的Mysql数据库中

use*_*739 2 mysql grails

我是Groovy&Grails的新手.我想提交解析CSV文件并导出到MySQL数据库的几个表中.我看了一些编码,但作为新手我很困惑.所以任何人都可以帮助我理解简单的csv文件解析和导出到MySQL数据库.

谢谢Sonu

hvg*_*des 11

Grails是一个引导程序,只要你的应用程序启动就会运行.它漂亮; 您可以将其配置为在不同环境中执行不同的操作.

一种方法是在bootstrap中执行以下操作:

1)读取csv文件,随时创建Domain对象.
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()}")
}
Run Code Online (Sandbox Code Playgroud)