使用groovy Sql批量插入? [英] Batch insert using groovy Sql?
问题描述
如何在模拟预处理语句的同时使用groovy Sql进行批量插入?我发现的所有示例与以下内容类似,不使用预先准备的语句。
How can you do a batch insert using groovy Sql while simulating prepared statements? All the examples I've found are similar to the following and don't use prepared statements.
withBatch { stmt ->
stmt.addBatch("insert into table (field1,field2) values('value1','value2')")
stmt.addBatch("insert into table (field1,field2) values('value3','value4')")
}
据此链接 http://jira.codehaus.org/browse/GROOVY-3504 没有办法使用准备直接来自批次内的报表。什么是最好的方法来模拟这个,所以我可以避免编写自己的代码,以避免SQL注入?
According to this link http://jira.codehaus.org/browse/GROOVY-3504 there is no way to use prepared statements directly from within batch. What is the best way to simulate this so I can avoid having to write my own code to avoid sql injection?
推荐答案
Groovy 1.8 .1引入了对批处理语句的支持。简单的例子:
Groovy 1.8.1 introduced support for prepared statements with batching. Simple example:
sql.withBatch(20, """update some_table
set some_column = :newvalue
where id = :key """) { ps ->
mymap.each { k,v ->
ps.addBatch(key:k, newvalue:v)
}
}
另请参阅我的帖子: http://novyden.blogspot.com/2011/09/groovy-batch-prepared-statement-nice.html
这篇关于使用groovy Sql批量插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!