SQL update
Execute updates
Section titled “Execute updates”You can execute SQL updates with the @JdbcUpdate annotation, like
Invalid source.In this case the method return void.
If you need the number of updates returning by stmt.executeUpdate, you define the return value int:
Invalid source.Sometime you only want to known if something was changed , in this case you can return simple boolean:
Invalid source.Execute update and return generated values by GENERATED_KEYS
Section titled “Execute update and return generated values by GENERATED_KEYS”In this example we have the following table:
Invalid source.The id is auto increment and the name is set to $id_name in case it was null. First we have to define a recordwhich can hold both values:
Invalid source.To get the generated values back with the JDBC style, we need to add
@JdbcReturnGeneratedValues(JdbcReturnGeneratedValues.ReturnKind.GENERATED_KEYS)
to the method, like this:
Invalid source.We also support the PostgreSQL RETRUNING style, like this:
Invalid source.Batch update
Section titled “Batch update”The annotation processor also support to execute batches. The the following example we want to add a lot of addresses to the database:
Invalid source.With @JdbcUpdate you define the SQL. The method must not have any parameter and must return an interface which extends the Kaumei JdbcBatch like this:
Invalid source.The method in the interface has the parameter of the SQL. Now you can easily add as many address as you want.
The generated code will execute your batch after 1.000 calls to the add method and before the batch is close,
because there are 100 still not send to the database.
Invalid source.You can define a custom batch size with the annotation @JdbcBatchSize or if you need a dynamic size with a parameter of the startBatchInsert method, like the following two examples.
Invalid source.Note: You can define @JdbcBatchSize at the interface level, in this case it will apply to all batch method of that interface.