|
1
2
3
4
5
6
7 |
sql.withBatch
{ stmt -> mymap.each
{
k,v -> stmt.addBatch("""UPDATE
some_table SET
some_column = '${v}' WHERE
id = ${k} """) }} |
|
1
2
3
4
5
6
7
8 |
sql.withBatch(20,
"""UPDATE
some_table SET
some_column = ? WHERE
id = ? """)
{ ps -> mymap.each
{
k,v -> ps.addBatch(v,
k) }} |
API DOC:
public int[] withBatch(Closure closure)Performs the closure (containing batch operations) within a batch. Uses a batch size of zero, i.e. no automatic partitioning of batches. This
means that The
closure will be called with a single argument; the database statement
(actually a Use it like this: def updateCounts = sql.withBatch { stmt ->
stmt.addBatch("insert into TABLENAME ...")
stmt.addBatch("insert into TABLENAME ...")
stmt.addBatch("insert into TABLENAME ...")
...
}
For
integrity and performance reasons, you may wish to consider executing your
batch command(s) within a transaction: sql.withTransaction {
def result1 = sql.withBatch { ... }
...
}
|
public int[] withBatch(int batchSize, Closure closure)Performs the closure (containing batch operations) within a batch using a given batch size. After
every The
closure will be called with a single argument; the database statement
(actually a Use it like this for batchSize of 20: def updateCounts = sql.withBatch(20) { stmt ->
stmt.addBatch("insert into TABLENAME ...")
stmt.addBatch("insert into TABLENAME ...")
stmt.addBatch("insert into TABLENAME ...")
...
}
For
integrity and performance reasons, you may wish to consider executing your
batch command(s) within a transaction: sql.withTransaction {
def result1 = sql.withBatch { ... }
...
}
|
public int[] withBatch(String sql, Closure closure)Performs the closure (containing batch operations specific to an associated prepared statement) within a batch. Uses a batch size of zero, i.e. no automatic partitioning of batches. This
means that The
closure will be called with a single argument; the prepared statement
(actually a An example: def updateCounts = sql.withBatch('insert into TABLENAME(a, b, c) values (?, ?, ?)') { ps ->
ps.addBatch([10, 12, 5])
ps.addBatch([7, 3, 98])
ps.addBatch(22, 67, 11)
def partialUpdateCounts = ps.executeBatch() // optional interim batching
ps.addBatch(30, 40, 50)
...
}
For
integrity and performance reasons, you may wish to consider executing your
batch command(s) within a transaction: sql.withTransaction {
def result1 = sql.withBatch { ... }
...
}
|
public int[] withBatch(int batchSize, String sql, Closure closure)Performs the closure (containing batch operations specific to an associated prepared statement) within a batch using a given batch size. After
every The
closure will be called with a single argument; the prepared statement
(actually a Below is an example using a batchSize of 20: def updateCounts = sql.withBatch(20, 'insert into TABLENAME(a, b, c) values (?, ?, ?)') { ps ->
ps.addBatch(10, 12, 5) // varargs style
ps.addBatch([7, 3, 98]) // list
ps.addBatch([22, 67, 11])
...
}
Named
parameters (into maps or domain objects) are also supported: def updateCounts = sql.withBatch(20, 'insert into TABLENAME(a, b, c) values (:foo, :bar, :baz)') { ps ->
ps.addBatch([foo:10, bar:12, baz:5]) // map
ps.addBatch(foo:7, bar:3, baz:98) // Groovy named args allow outer brackets to be dropped
...
}
Named
ordinal parameters (into maps or domain objects) are also
supported: def updateCounts = sql.withBatch(20, 'insert into TABLENAME(a, b, c) values (?1.foo, ?2.bar, ?2.baz)') { ps ->
ps.addBatch([[foo:22], [bar:67, baz:11]]) // list of maps or domain objects
ps.addBatch([foo:10], [bar:12, baz:5]) // varargs allows outer brackets to be dropped
ps.addBatch([foo:7], [bar:3, baz:98])
...
}
// swap to batch size of 5 and illustrate simple and domain object cases ...
class Person { String first, last }
def updateCounts2 = sql.withBatch(5, 'insert into PERSON(id, first, last) values (?1, ?2.first, ?2.last)') { ps ->
ps.addBatch(1, new Person(first:'Peter', last:'Pan'))
ps.addBatch(2, new Person(first:'Snow', last:'White'))
...
}
For
integrity and performance reasons, you may wish to consider executing your
batch command(s) within a transaction: sql.withTransaction {
def result1 = sql.withBatch { ... }
...
}
|
MySql支持Batch Sql的参数设定:
MySQL does have batch processing support though, it just requires different SQL.
The MySQL JDBC driver does support this, but requires the JDBC connect property to be set.
This can easily be set by modifying your connect URL, such as;