如何正确使用 AddBatch/withBatch 将 xml 标签值批量插入数据库 table
How to use AddBatch/withBatch properly for bulk inserting xml tag value to database table
'innerXml' 是一个包含大量 xml 标签的 xml 文件。我正在尝试获取标签值并将它们转储到数据库 table 中。
我试过下面的代码,它工作正常。
innerXml.Row.each { Row ->
sql.execute("INSERT INTO tab1(col1,col2) VALUES (${Row.Column0.text()},${Row.Column1.text()} )")
但是由于有大量的 xml 标签,它会导致性能问题,无法逐一插入记录。正如专家建议的那样,我尝试使用 withBatch 来改进 performance.Please 找到我尝试使用 withBatch 的以下代码:
sql.withBatch(386, """ insert into tab1(col1,col2) values (?, ?) """) { ps -> innerXml.Row.each { Row ->ps.addBatch(${Row.Column0.text()} ,${Row.Column1.text()}) }}
但我遇到以下错误:
groovy.lang.MissingMethodException: No signature of method: Script41.$() is applicable for argument types: (Script41$_run_closure2_closure3_closure4) values: [Script41$_run_closure2_closure3_closure4@23724e8d] Possible solutions: is(java.lang.Object), run(), run(), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure) error at line: 38
请帮忙
提前致谢!!
您需要做的:
// 386 is an odd batch size?
sql.withBatch(386, 'insert into tab1(col1,col2) values (?, ?)') { ps ->
innerXml.Row.each { row ->
ps.addBatch(row.Column0.text(), row.Column1.text())
}
}
'innerXml' 是一个包含大量 xml 标签的 xml 文件。我正在尝试获取标签值并将它们转储到数据库 table 中。 我试过下面的代码,它工作正常。
innerXml.Row.each { Row ->
sql.execute("INSERT INTO tab1(col1,col2) VALUES (${Row.Column0.text()},${Row.Column1.text()} )")
但是由于有大量的 xml 标签,它会导致性能问题,无法逐一插入记录。正如专家建议的那样,我尝试使用 withBatch 来改进 performance.Please 找到我尝试使用 withBatch 的以下代码:
sql.withBatch(386, """ insert into tab1(col1,col2) values (?, ?) """) { ps -> innerXml.Row.each { Row ->ps.addBatch(${Row.Column0.text()} ,${Row.Column1.text()}) }}
但我遇到以下错误:
groovy.lang.MissingMethodException: No signature of method: Script41.$() is applicable for argument types: (Script41$_run_closure2_closure3_closure4) values: [Script41$_run_closure2_closure3_closure4@23724e8d] Possible solutions: is(java.lang.Object), run(), run(), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure) error at line: 38
请帮忙
提前致谢!!
您需要做的:
// 386 is an odd batch size?
sql.withBatch(386, 'insert into tab1(col1,col2) values (?, ?)') { ps ->
innerXml.Row.each { row ->
ps.addBatch(row.Column0.text(), row.Column1.text())
}
}