Camel Component: MyBatis - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

The MyBatis component allows you to query, poll, insert, update and delete data in a relational database using MyBatis.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-mybatis</artifactId>
   <!-- use the same version as your Camel core version -->
   <version>x.x.x</version>
</dependency>

URI format and Options

mybatis:statementName[?options]

Where statementName is the statement name in the MyBatis XML configuration file which maps to the query, insert, update or delete operation you wish to evaluate.

You can append query options to the URI in the following format, ?option=value&option=value&..., where option can be:

Table 17. 

Option

Type

Default

Description

consumer.onConsume

String

null

Statements to run after consuming. Can be used, for example, to update rows after they have been consumed and processed in Camel. Multiple statements can be separated with commas.

consumer. useIterator

boolean

true

If true each row returned when polling will be processed individually. If false the entire List of data is set as the IN body.

consumer. routeEmptyResultSet

boolean

false

Sets whether empty result sets should be routed.

statementType

StatementType

null

Mandatory to specify for the Producer to control which kind of operation to invoke. The enum values are: SelectOne, SelectList, Insert, InsertList, Update, UpdateList, Delete, DeleteList.

maxMessagesPerPoll

int

0

An integer to define the maximum messages to gather per poll. By default, no maximum is set. Can be used to set a limit of, for example, 1000 to avoid when starting up the server that there are thousands of files. Set a value of 0 or negative to disable it.


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.

Message Headers

Camel will populate the result message, either IN or OUT with a header with the statement used:

Header

Type

Description

CamelMyBatis-StatementName

String

The statementName used (for example: insertAccount).

CamelMyBatisResult

Object

The response returned from MyBatis in any of the operations. For instance an INSERT could return the auto-generated key, or number of rows etc.

Message Body

The response from MyBatis will only be set as body if it is 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.

Samples

For example if you wish to consume beans from a JMS queue and insert them into a database you could do the following:

from("activemq:queue:newAccount")
   .to("mybatis:insertAccount?statementType=Insert");

Notice we have to specify the statementType, as we need to instruct Camel which kind of operation to invoke. The insertAccount value given above is the MyBatis ID in the SQL map file:

<!-- Insert example, using the Account parameter class -->
<insert id="insertAccount" parameterClass="Account">
   insert into ACCOUNT (
      ACC_ID,
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
   ) values (
      #id#, #firstName#, #lastName#, #emailAddress#
   )
</insert>

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:

from("direct:start")
   .to("mybatis:selectAccountById?statementType=QueryForObject")
   .to("mock:result");

In the code above we 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 :

from("direct:start")
   .to("mybatis:selectAllAccounts?statementType=SelectList")
   .to("mock:result");

And the same for UPDATE, where we can send an Account object as the IN body to MyBatis:

from("direct:start")
   .to("mybatis:updateAccount?statementType=Update")
   .to("mock:result");

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 executing the consumeAccount statement after the 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.

from("mybatis:selectUnprocessedAccounts?consumer.
   onConsume=consumeAccount").to("mock:results");

And the statements in the sqlmap file:

<select id="selectUnprocessedAccounts" resultMap="AccountResult">
   select * from ACCOUNT where PROCESSED = false
</select>
<update id="consumeAccount" parameterClass="Account">
   update ACCOUNT set PROCESSED = true where ACC_ID = #id#
</update>