camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > iBATIS
Date Thu, 17 Dec 2009 15:50:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/iBATIS">iBATIS</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="iBATIS-iBATIS"></a>iBATIS</h2>

<p>The <b>ibatis:</b> component allows you to query, poll, insert, update
and delete data in a relational database using <a href="http://ibatis.apache.org/" rel="nofollow">Apache
iBATIS</a>.</p>

<h3><a name="iBATIS-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ibatis:statementName[?options]
</pre>
</div></div>

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

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<p>This component will by default load the iBatis SqlMapConfig file from the root of
the classpath and expected named as <tt>SqlMapConfig.xml</tt>.<br/>
It uses Spring resource loading so you can define it using <tt>classpath</tt>,
<tt>file</tt> or <tt>http</tt> as prefix to load resources with those
schemes.<br/>
In Camel 2.2 you can configure this on the iBatisComponent with the <tt>setSqlMapConfig(String)</tt>
method.</p>

<h3><a name="iBATIS-Options"></a>Options</h3>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.onConsume</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> 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. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.useIterator</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> If <tt>true</tt> each row returned when polling
will be processed individually. If <tt>false</tt> the entire <tt>List</tt>
of data is set as the IN body. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.routeEmptyResultSet</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> Sets whether empty result set
should be routed or not. By default, empty result sets are not routed. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>statementType</tt> </td>
<td class='confluenceTd'> <tt>StatementType</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 1.6.1/2.0:</b> Mandatory to specify for
IbatisProducer to control which iBatis <tt>SqlMapClient</tt> method to invoke.
The enum values are: <tt>QueryForObject</tt>, <tt>QueryForList</tt>,
<tt>Insert</tt>, <tt>Update</tt>, <tt>Delete</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxMessagesPerPoll</tt> </td>
<td class='confluenceTd'> <tt>int</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> 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. </td>
</tr>
</tbody></table>


<h3><a name="iBATIS-MessageHeaders"></a>Message Headers</h3>
<p>Camel will populate the result message, either IN or OUT with a header with the operationName
used:</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>org.apache.camel.ibatis.queryName</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Camel 1.x: The <b>statementName</b> used (for
example: insertAccount). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelIBatisStatementName</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Camel 2.0: The <b>statementName</b> used (for
example: insertAccount). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelIBatisResult</tt> </td>
<td class='confluenceTd'> <tt>Object</tt> </td>
<td class='confluenceTd'> Camel 1.6.2/2.0: The <b>response</b> returned
from iBatis in any of the operations. For instance an <tt>INSERT</tt> could return
the auto-generated key, or number of rows etc. </td>
</tr>
</tbody></table>

<h3><a name="iBATIS-MessageBody"></a>Message Body</h3>
<p><b>Camel 1.6.1</b>: The response from iBatis will be set as OUT body
<br/>
<b>Camel 1.6.2/2.0</b>: The response from iBatis will only be set as body if it's
a <tt>SELECT</tt> statement. That means, for example, for <tt>INSERT</tt>
statements Camel will not replace the body. This allows you to continue routing and keep the
original body. The response from iBatis is always stored in the header with the key <tt>CamelIBatisResult</tt>.</p>

<h3><a name="iBATIS-Samples"></a>Samples</h3>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:queue:newAccount"</span>).
  to(<span class="code-quote">"ibatis:insertAccount?statementType=Insert"</span>);
</pre>
</div></div>

<p>Notice we have to specify the <tt>statementType</tt>, as we need to instruct
Camel which <tt>SqlMapClient</tt> operation to invoke.</p>

<p>Where <b>insertAccount</b> is the iBatis ID in the SQL map file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag"><span class="code-comment">&lt;!-- Insert example,
using the Account parameter class --&gt;</span></span>
  <span class="code-tag">&lt;insert id=<span class="code-quote">"insertAccount"</span>
parameterClass=<span class="code-quote">"Account"</span>&gt;</span>
    insert into ACCOUNT (
      ACC_ID,
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
    )
    values (
      #id#, #firstName#, #lastName#, #emailAddress#
    )
  <span class="code-tag">&lt;/insert&gt;</span>
</pre>
</div></div>

<h3><a name="iBATIS-UsingStatementTypeforbettercontrolofIBatis"></a>Using
StatementType for better control of IBatis</h3>
<p><b>Available as of Camel 1.6.1/2.0</b><br/>
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 <tt>SELEECT</tt>, <tt>UPDATE</tt>,
<tt>DELETE</tt> or <tt>INSERT</tt> 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 <tt>SELECT</tt> statement we can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"ibatis:selectAccountById?statementType=QueryForObject"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>In the code above we can invoke the iBatis statement <tt>selectAccountById</tt>
and the IN body should contain the account id we want to retrieve, such as an <tt>Integer</tt>
type.</p>

<p>We can do the same for some of the other operations, such as <tt>QueryForList</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"ibatis:selectAllAccounts?statementType=QueryForList"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And the same for <tt>UPDATE</tt>, where we can send an <tt>Account</tt>
object as IN body to iBatis:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"ibatis:updateAccount?statementType=Update"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>


<h4><a name="iBATIS-Scheduledpollingexample"></a>Scheduled polling example</h4>
<p>Since this component does not support scheduled polling, you need to use another
mechanism for triggering the scheduled polls, such as the <a href="/confluence/display/CAMEL/Timer"
title="Timer">Timer</a> or <a href="/confluence/display/CAMEL/Quartz" title="Quartz">Quartz</a>
components.</p>

<p>In the sample below we poll the database, every 30 seconds using the <a href="/confluence/display/CAMEL/Timer"
title="Timer">Timer</a> component and send the data to the JMS queue:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"timer:<span class="code-comment">//pollTheDatabase?delay=30000"</span>).to(<span
class="code-quote">"ibatis:selectAllAccounts?statementType=QueryForList"</span>).to(<span
class="code-quote">"activemq:queue:allAccounts"</span>);</span>
</pre>
</div></div>

<p>And the iBatis SQL map file used:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag"><span class="code-comment">&lt;!-- Select with no
parameters using the result map for Account class. --&gt;</span></span>
  <span class="code-tag">&lt;select id=<span class="code-quote">"selectAllAccounts"</span>
resultMap=<span class="code-quote">"AccountResult"</span>&gt;</span>
    select * from ACCOUNT
  <span class="code-tag">&lt;/select&gt;</span>
</pre>
</div></div>

<h4><a name="iBATIS-UsingonConsume"></a>Using onConsume</h4>
<p>This component supports executing statements <b>after</b> data have been
consumed and processed by Camel. This allows you to do post updates in the database. Notice
all statements must be <tt>UPDATE</tt> statements. Camel supports executing multiple
statements whose name should be separated by comma.</p>

<p>The route below illustrates we execute the <b>consumeAccount</b> 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.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"ibatis:selectUnprocessedAccounts?consumer.onConsume=consumeAccount"</span>).to(<span
class="code-quote">"mock:results"</span>);
</pre>
</div></div>

<p>And the statements in the sqlmap file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;select id=<span class="code-quote">"selectUnprocessedAccounts"</span>
resultMap=<span class="code-quote">"AccountResult"</span>&gt;</span>
     select * from ACCOUNT where PROCESSED = false
<span class="code-tag">&lt;/select&gt;</span>
</pre>
</div></div>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;update id=<span class="code-quote">"consumeAccount"</span>
parameterClass=<span class="code-quote">"Account"</span>&gt;</span>
     update ACCOUNT set PROCESSED = true where ACC_ID = #id#
<span class="code-tag">&lt;/update&gt;</span>
</pre>
</div></div>

<h3><a name="iBATIS-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/iBATIS">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=67664&revisedVersion=21&originalVersion=20">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/iBATIS?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message