...
Maven users will need to add the following dependency to their pom.xml
for this component:
...
URI format
...
...
Where statementName is the statement name in the MyBatis XML mapping file which maps to the query, insert, update or delete operation you wish to evaluate.
...
This component will by default load the MyBatis SqlMapConfig file from the root of the classpath with the expected name of SqlMapConfig.xml
.
If the file is located in another location, you will need to configure the configurationUri
option on the MyBatisComponent
component.
Options
...
...
Option | Type | Default | Description |
---|---|---|---|
|
|
| Statements to run after consuming. Can be used, for example, to update rows after they have been consumed and processed in Camel. See sample later. Multiple statements can be separated with commas. |
|
|
| If |
|
|
| Sets whether empty result sets should be routed. |
|
|
| Mandatory to specify for the producer to control which kind of operation to invoke. The enum values are: |
|
|
| This option is intended to split results returned by the database pool into the batches and deliver them in multiple exchanges. This integer defines the maximum messages to deliver in single exchange. By default, no maximum is set. Can be used to set a limit of e.g. 1000 to avoid when starting up the server that there are thousands of files. Set a value of 0 or negative to disable it. |
|
|
| Camel 2.11: The executor type to be used while executing statements. The supported values are: simple, reuse, batch. By default, the value is not specified and is equal to what MyBatis uses, i.e. simple. |
| String | null | Camel 2.15: To store the result as a header instead of the message body. This allows to preserve the existing message body as-is. |
...
inputHeader | String | null | Camel 2.15: "inputHeader" parameter to use a header value as input to the component instead of the body. |
transacted | boolean | false | Camel 2.16.2: SQL consumer only:Enables or disables transaction. If enabled then if processing an exchange failed then the consumer break out processing any further exchanges to cause a rollback eager |
Message Headers
Camel will populate
Message Headers
Camel will populate the result message, either IN or OUT with a header with the statement used:
...
...
Header | Type | Description |
---|---|---|
|
| The statementName used (for example: insertAccount). |
|
| The response returned from MtBatis in any of the operations. For instance an |
Message Body
The response from MyBatis will only be set as the body if it's a SELECT
statement. That means, for example, for INSERT
statements Camel will not replace the body. This allows you to continue routing and keep the original body. The response from MyBatis is always stored in the header with the key CamelMyBatisResult
.
...
For example if you wish to consume beans from a JMS queue and insert them into a database you could do the following:
...
...
Notice we have to specify the statementType
, as we need to instruct Camel which kind of operation to invoke.
Where insertAccount is the MyBatis ID in the SQL mapping file:
...
Using StatementType for better control of MyBatis
When routing to an MyBatis endpoint you will want more fine grained control so you can control whether the SQL statement to be executed is a SELECT
, UPDATE
, DELETE
or INSERT
etc. So for instance if we want to route to an MyBatis endpoint in which the IN body contains parameters to a SELECT
statement we can do:
...
...
In the code above we can invoke the MyBatis statement selectAccountById
and the IN body should contain the account id we want to retrieve, such as an Integer
type.
We can do the same for some of the other operations, such as SelectList
:
...
...
And the same for UPDATE
, where we can send an Account
object as the IN body to MyBatis:
...
Using InsertList StatementType
...
MyBatis allows you to insert multiple rows using its for-each batch driver. To use this, you need to use the <foreach> in the mapper XML file. For example as shown below:
...
as shown below:
...
Then you can insert multiple rows, by sending a Camel message to the mybatis
endpoint which uses the InsertList
statement type, as shown below:
...
Using UpdateList StatementType
...
MyBatis allows you to update multiple rows using its for-each batch driver. To use this, you need to use the <foreach> in the mapper XML file. For example as shown below:
...
...
Then you can update multiple rows, by sending a Camel message to the mybatis endpoint which uses the UpdateList statement type, as shown below:
...
...
Using DeleteList StatementType
...
MyBatis allows you to delete multiple rows using its for-each batch driver. To use this, you need to use the <foreach> in the mapper XML file. For example as shown below:
...
...
Then you can delete multiple rows, by sending a Camel message to the mybatis endpoint which uses the DeleteList statement type, as shown below:
...
...
Notice on InsertList, UpdateList and DeleteList StatementTypes
...
This component supports scheduled polling and can therefore be used as a Polling Consumer. For example to poll the database every minute:
...
...
See "ScheduledPollConsumer Options" on Polling Consumer for more options.
Alternatively you can use another mechanism for triggering the scheduled polls, such as the Timer or Quartz components. In the sample below we poll the database, every 30 seconds using the Timer component and send the data to the JMS queue:
...
...
And the MyBatis SQL mapping file used:
...
...
Using onConsume
This component supports executing statements after data have been consumed and processed by Camel. This allows you to do post updates in the database. Notice all statements must be UPDATE
statements. Camel supports executing multiple statements whose names should be separated by commas.
The route below illustrates we execute the consumeAccount statement data is processed. This allows us to change the status of the row in the database to processed, so we avoid consuming it twice or more.
...
...
And the statements in the sqlmap file:
...
...
Participating in transactions
...
The first part requires the setup of a DataSource
. This is typically a pool (either DBCP, or c3p0), which needs to be wrapped in a Spring proxy. This proxy enables non-Spring use of the DataSource
to participate in Spring transactions (the MyBatis SqlSessionFactory
does just this).
...
...
This has the additional benefit of enabling the database configuration to be externalised using property placeholders.
A transaction manager is then configured to manage the outermost DataSource
:
...
...
A mybatis-spring SqlSessionFactoryBean
then wraps that same DataSource
:
...
...
The camel-mybatis component is then configured with that factory:
...
...
Finally, a transaction policy is defined over the top of the transaction manager, which can then be used as usual:
...
...
Endpoint See AlsoEndpoint See Also Include Page