You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 16 Next »

iBATIS

The ibatis: component allows you to query, poll, insert, update and delete data in a relational database using Apache iBATIS.

URI format

ibatis:statementName[?options]

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

Options

Option

Type

Default

Description

consumer.onConsume

String

null

Statements to run after consuming. Can be used to eg. 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

Camel 2.0: Sets whether empty resultset should be routed or not. By default empty result sets are not routed.

statementType

StatementType

null

Camel 1.6.1/2.0: Mandatory to specify for IbatisProducer to control which iBatis SqlMapClient method to invoke. The enum values are: QueryForObject, QueryForList, Insert, Update, Delete.

Message Headers

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

Header

Type

Description

org.apache.camel.ibatis.queryName

String

Camel 1.x: The statementName used (for example: insertAccount)

CamelIBatisStatementName

String

Camel 2.0: The statementName used (for example: insertAccount)

Samples

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

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

Notice we have to specify the statementType, as we need to instruct Camel which SqlMapClient operation to invoke.

Where insertAccount is the iBatis 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 IBatis

Available as of Camel 1.6.1/2.0
When routing to an iBatis endpoint you want more fine grained control so you can control whether the SQL statement to be executed is a SELEECT, UPDATE, DELETE or INSERT etc. This is now possible in Camel 1.6.1/2.0. So for instance if we want to route to an iBatis endpoint in which the IN body contains parameters to a SELECT statement we can do:

Error formatting macro: snippet: java.lang.NullPointerException

In the code above we can invoke the iBatis 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 QueryForList:

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

Scheduled polling example

Since this component does not support scheduled polling you need to use another mechanism for triggering the scheduled pools such as the Timer or Quartz components.

In the sample below we poll the database, every 30th second using the Timer component and sends the data to the JMS queue:

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

And the iBatis SQL map file used:

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

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 name should be separated by comma.

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.

Error formatting macro: snippet: java.lang.NullPointerException

And the statements in the sqlmap file:

Error formatting macro: snippet: java.lang.NullPointerException
Error formatting macro: snippet: java.lang.NullPointerException
  • No labels