LibraryToggle FramesPrintFeedback

Available as of Camel 2.7

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>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
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. See sample later. Multiple statements can be separated with comma.
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 set should be routed or not. By default, empty result sets are not routed.
statementType StatementType null Mandatory to specify for producer to control which kind of operation to invoke. The enum values are: SelectOne, SelectList, Insert, Update, Delete.
maxMessagesPerPoll int 0 An integer to define a maximum messages to gather per poll. 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 disabled it.

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

Header Type Description
CamelMyBatisStatementName String The statementName used (for example: insertAccount).
CamelMyBatisResult Object The response returned from MtBatis in any of the operations. For instance an INSERT could return the auto-generated key, or number of rows etc.

Since this component does not support scheduled polling, you need to 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:

from("timer://pollTheDatabase?delay=30000").to("mbatis:selectAllAccounts").to("activemq:queue:allAccounts");

And the MyBatis SQL mapping file used:

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllAccounts" resultMap="AccountResult">
    select * from ACCOUNT
  </select>
Comments powered by Disqus