Modified: websites/production/camel/content/mybatis.html
==============================================================================
--- websites/production/camel/content/mybatis.html (original)
+++ websites/production/camel/content/mybatis.html Fri Aug 25 18:20:06 2017
@@ -36,17 +36,6 @@
<![endif]-->
- <link href='//camel.apache.org/styles/highlighter/styles/shCoreCamel.css'
rel='stylesheet' type='text/css' />
- <link href='//camel.apache.org/styles/highlighter/styles/shThemeCamel.css'
rel='stylesheet' type='text/css' />
- <script src='//camel.apache.org/styles/highlighter/scripts/shCore.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushJava.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushXml.js'
type='text/javascript'></script>
- <script src='//camel.apache.org/styles/highlighter/scripts/shBrushPlain.js'
type='text/javascript'></script>
-
- <script type="text/javascript">
- SyntaxHighlighter.defaults['toolbar'] = false;
- SyntaxHighlighter.all();
- </script>
<title>
Apache Camel: MyBatis
@@ -86,30 +75,17 @@
<tbody>
<tr>
<td valign="top" width="100%">
-<div class="wiki-content maincontent"><h2
id="MyBatis-MyBatis">MyBatis</h2><p><strong>Available as of Camel
2.7</strong></p><p>The <strong>mybatis:</strong> component allows you to query,
poll, insert, update and delete data in a relational database using <a
shape="rect" class="external-link" href="http://mybatis.org/"
rel="nofollow">MyBatis</a>.</p><p>Maven users will need to add the following
dependency to their <code>pom.xml</code> for this component:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<dependency>
+<div class="wiki-content maincontent"><h2
id="MyBatis-MyBatis">MyBatis</h2><p><strong>Available as of Camel
2.7</strong></p><p>The <strong>mybatis:</strong> component allows you to query,
poll, insert, update and delete data in a relational database using <a
shape="rect" class="external-link" href="http://mybatis.org/"
rel="nofollow">MyBatis</a>.</p><p>Maven users will need to add the following
dependency to their <code>pom.xml</code> for this component:</p><parameter
ac:name="">xml</parameter><plain-text-body><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>
-]]></script>
-</div></div><h3 id="MyBatis-URIformat">URI format</h3><div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[mybatis:statementName[?options]
-]]></script>
-</div></div><p>Where <strong>statementName</strong> is the statement name in
the MyBatis XML mapping 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,
<code>?option=value&option=value&...</code></p><p>This component will
by default load the MyBatis SqlMapConfig file from the root of the classpath
with the expected name of <code>SqlMapConfig.xml</code>.<br clear="none"> If
the file is located in another location, you will need to configure the
<code>configurationUri</code> option on the <code>MyBatisComponent</code>
component.</p><h3 id="MyBatis-Options">Options</h3><div
class="confluenceTableSmall"><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh"><p>Option</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Default</p></th><th colsp
an="1" rowspan="1" class="confluenceTh"><p>Description</p></th></tr><tr><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>consumer.onConsume</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>String</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>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.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>consumer.useIterator</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>boolean</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>If <code>true</code> each row returned when
polling will be processed individually. If <code>false</cod
e> the entire <code>List</code> of data is set as the IN
body.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>consumer.routeEmptyResultSet</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>boolean</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Sets whether empty result sets should be
routed.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>statementType</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>StatementType</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Mandatory to specify for the
producer to control which kind of operation to invoke. The enum values are:
<code>SelectOne</code>, <code>SelectList</code>, <code>Insert</code>,
<code>InsertList</code>, <code>Update</code>, <code>Update
List</code>, <code>Delete</code>, and <code>DeleteList</code>.
<strong>Notice:</strong> <code>InsertList</code> is available as of Camel 2.10,
and <code>UpdateList</code>, <code>DeleteList</code> is available as of Camel
2.11.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>maxMessagesPerPoll</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>int</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>0</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>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.</p></td></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p>
<code>executorType</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>String</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>null</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p><strong>Camel 2.11:</strong> 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. <strong>simple</strong>. <br clear="none"
class="atl-forced-newline"> <strong>simple</strong> executor does nothing
special. <br clear="none" class="atl-forced-newline"> <strong>reuse</strong>
executor reuses prepared statements. <br clear="none"
class="atl-forced-newline"> <strong>batch</strong> executor reuses statements
and batches updates.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>outputHeader</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><code>String</code></td><td colspan="1" ro
wspan="1" class="confluenceTd"><code>null</code></td><td colspan="1"
rowspan="1" class="confluenceTd"><strong>Camel 2.15:</strong><span
style="color: rgb(0,0,0);"> To store the result as a header instead of the
message body. This allows to preserve the existing message body
as-is.</span></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><code>inputHeader</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>String</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>null</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><strong>Camel 2.15:  </strong>"inputHeader" parameter
to use a header value as input to the component instead of the
body.</td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><code>transacted</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>boolean</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>false</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><stron
g>Camel 2.16.2:</strong><span> </span><strong>SQL consumer
only:</strong><span>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</span></td></tr></tbody></table></div></div>
-
-
-<h3 id="MyBatis-MessageHeaders">Message Headers</h3><p>Camel will populate the
result message, either IN or OUT with a header with the statement used:</p><div
class="confluenceTableSmall"><div class="table-wrap"><table
class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1"
class="confluenceTh"><p>Header</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1"
class="confluenceTd"><p><code>CamelMyBatisStatementName</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>String</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>The
<strong>statementName</strong> used (for example:
insertAccount).</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>CamelMyBatisResult</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confl
uenceTd"><p>The <strong>response</strong> returned from MtBatis in any of the
operations. For instance an <code>INSERT</code> could return the auto-generated
key, or number of rows etc.</p></td></tr></tbody></table></div></div>
-
-
-<h3 id="MyBatis-MessageBody">Message Body</h3><p>The response from MyBatis
will only be set as the body if it's a <code>SELECT</code> statement. That
means, for example, for <code>INSERT</code> 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
<code>CamelMyBatisResult</code>.</p><h3 id="MyBatis-Samples">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 pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("activemq:queue:newAccount").
- to("mybatis:insertAccount?statementType=Insert");
-]]></script>
-</div></div><p>Notice we have to specify the <code>statementType</code>, as we
need to instruct Camel which kind of operation to invoke.</p><p>Where
<strong>insertAccount</strong> is the MyBatis ID in the SQL mapping
file:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <!-- Insert example, using the Account
parameter class -->
- <insert id="insertAccount" parameterType="Account">
+</plain-text-body><h3 id="MyBatis-URIformat">URI
format</h3><plain-text-body>mybatis:statementName[?options]
+</plain-text-body><p>Where <strong>statementName</strong> is the statement
name in the MyBatis XML mapping 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,
<code>?option=value&option=value&...</code></p><p>This component will
by default load the MyBatis SqlMapConfig file from the root of the classpath
with the expected name of <code>SqlMapConfig.xml</code>.<br clear="none"> If
the file is located in another location, you will need to configure the
<code>configurationUri</code> option on the <code>MyBatisComponent</code>
component.</p><h3 id="MyBatis-Options">Options</h3><parameter
ac:name="class">confluenceTableSmall</parameter><rich-text-body><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh"><p>Option</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1" cl
ass="confluenceTh"><p>Default</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>consumer.onConsume</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>String</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>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.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>consumer.useIterator</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>boolean</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>true</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>If <code>true</code> each row returned when
polling will
be processed individually. If <code>false</code> the entire <code>List</code>
of data is set as the IN body.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>consumer.routeEmptyResultSet</code></p></td><td
colspan="1" rowspan="1"
class="confluenceTd"><p><code>boolean</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>false</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>Sets whether empty result sets should be
routed.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>statementType</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>StatementType</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td
colspan="1" rowspan="1" class="confluenceTd"><p>Mandatory to specify for the
producer to control which kind of operation to invoke. The enum values are:
<code>SelectOne</code>, <code>SelectList</code>, <code>Insert</code>,
<code>Inser
tList</code>, <code>Update</code>, <code>UpdateList</code>,
<code>Delete</code>, and <code>DeleteList</code>. <strong>Notice:</strong>
<code>InsertList</code> is available as of Camel 2.10, and
<code>UpdateList</code>, <code>DeleteList</code> is available as of Camel
2.11.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>maxMessagesPerPoll</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>int</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>0</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>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.</p></td></tr><tr><td c
olspan="1" rowspan="1"
class="confluenceTd"><p><code>executorType</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>String</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>null</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><strong>Camel 2.11:</strong> 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. <strong>simple</strong>. <br clear="none"
class="atl-forced-newline"> <strong>simple</strong> executor does nothing
special. <br clear="none" class="atl-forced-newline"> <strong>reuse</strong>
executor reuses prepared statements. <br clear="none"
class="atl-forced-newline"> <strong>batch</strong> executor reuses statements
and batches updates.</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>outputHeader</code></p></td><td colspan="1"
rowspan="1" class="confluenc
eTd"><code>String</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>null</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><strong>Camel 2.15:</strong><span style="color:
rgb(0,0,0);"> To store the result as a header instead of the message body.
This allows to preserve the existing message body
as-is.</span></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><code>inputHeader</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>String</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>null</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><strong>Camel 2.15:  </strong>"inputHeader" parameter
to use a header value as input to the component instead of the
body.</td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><code>transacted</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>boolean</code></td><td colspan="1" rowspan="1"
class="confluenceTd"><code>false</code></td><td cols
pan="1" rowspan="1" class="confluenceTd"><strong>Camel
2.16.2:</strong><span> </span><strong>SQL consumer
only:</strong><span>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</span></td></tr></tbody></table></div></rich-text-body><h3
id="MyBatis-MessageHeaders">Message Headers</h3><p>Camel will populate the
result message, either IN or OUT with a header with the statement
used:</p><parameter
ac:name="class">confluenceTableSmall</parameter><rich-text-body><div
class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1"
rowspan="1" class="confluenceTh"><p>Header</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Type</p></th><th colspan="1" rowspan="1"
class="confluenceTh"><p>Description</p></th></tr><tr><td colspan="1"
rowspan="1"
class="confluenceTd"><p><code>CamelMyBatisStatementName</code></p></td><td
colspan="1" rowspan="1" class="
confluenceTd"><p><code>String</code></p></td><td colspan="1" rowspan="1"
class="confluenceTd"><p>The <strong>statementName</strong> used (for example:
insertAccount).</p></td></tr><tr><td colspan="1" rowspan="1"
class="confluenceTd"><p><code>CamelMyBatisResult</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p><code>Object</code></p></td><td colspan="1"
rowspan="1" class="confluenceTd"><p>The <strong>response</strong> returned from
MtBatis in any of the operations. For instance an <code>INSERT</code> could
return the auto-generated key, or number of rows
etc.</p></td></tr></tbody></table></div></rich-text-body><h3
id="MyBatis-MessageBody">Message Body</h3><p>The response from MyBatis will
only be set as the body if it's a <code>SELECT</code> statement. That means,
for example, for <code>INSERT</code> 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 <code>CamelMyBatisResult</code>.</p><h3
id="MyBatis-Samples">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><plain-text-body>from("activemq:queue:newAccount").
+ to("mybatis:insertAccount?statementType=Insert");
+</plain-text-body><p>Notice we have to specify the <code>statementType</code>,
as we need to instruct Camel which kind of operation to invoke.</p><p>Where
<strong>insertAccount</strong> is the MyBatis ID in the SQL mapping
file:</p><parameter ac:name="">xml</parameter><plain-text-body> <!-- Insert
example, using the Account parameter class -->
+ <insert id="insertAccount" parameterType="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
@@ -120,153 +96,70 @@
#{id}, #{firstName}, #{lastName}, #{emailAddress}
)
</insert>
-]]></script>
-</div></div><h3 id="MyBatis-UsingStatementTypeforbettercontrolofMyBatis">Using
StatementType for better control of MyBatis</h3><p>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 <code>SELECT</code>, <code>UPDATE</code>,
<code>DELETE</code> or <code>INSERT</code> etc. So for instance if we want to
route to an MyBatis endpoint in which the IN body contains parameters to a
<code>SELECT</code> statement we can do:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start")
- .to("mybatis:selectAccountById?statementType=SelectOne")
- .to("mock:result");
-]]></script>
-</div></div>In the code above we can invoke the MyBatis statement
<code>selectAccountById</code> and the IN body should contain the account id we
want to retrieve, such as an <code>Integer</code> type.<p>We can do the same
for some of the other operations, such as <code>SelectList</code>:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start")
- .to("mybatis:selectAllAccounts?statementType=SelectList")
- .to("mock:result");
-]]></script>
-</div></div>And the same for <code>UPDATE</code>, where we can send an
<code>Account</code> object as the IN body to MyBatis:<div class="code panel
pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start")
- .to("mybatis:updateAccount?statementType=Update")
- .to("mock:result");
-]]></script>
-</div></div><h4 id="MyBatis-UsingInsertListStatementType">Using InsertList
StatementType</h4><p><strong>Available as of Camel 2.10</strong></p><p>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:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<!-- Batch Insert example, using the Account parameter class -->
-<insert id="batchInsertAccount"
parameterType="java.util.List">
- insert into ACCOUNT (
- ACC_ID,
- ACC_FIRST_NAME,
- ACC_LAST_NAME,
- ACC_EMAIL
- )
- values (
- <foreach item="Account" collection="list"
open="" close="" separator="),(">
- #{Account.id}, #{Account.firstName}, #{Account.lastName},
#{Account.emailAddress}
- </foreach>
- )
-</insert>
-]]></script>
-</div></div>Then you can insert multiple rows, by sending a Camel message to
the <code>mybatis</code> endpoint which uses the <code>InsertList</code>
statement type, as shown below:<div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("direct:start")
- .to("mybatis:batchInsertAccount?statementType=InsertList")
- .to("mock:result");
-]]></script>
-</div></div><h4 id="MyBatis-UsingUpdateListStatementType">Using UpdateList
StatementType</h4><p><strong>Available as of Camel 2.11</strong></p><p>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:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<update id="batchUpdateAccount"
parameterType="java.util.Map">
+</plain-text-body><h3
id="MyBatis-UsingStatementTypeforbettercontrolofMyBatis">Using StatementType
for better control of MyBatis</h3><p>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 <code>SELECT</code>, <code>UPDATE</code>,
<code>DELETE</code> or <code>INSERT</code> etc. So for instance if we want to
route to an MyBatis endpoint in which the IN body contains parameters to a
<code>SELECT</code> statement we can
do:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisSelectOneTest.java}</plain-text-body>In
the code above we can invoke the MyBatis statement
<code>selectAccountById</code> and the IN body should contain the account id we
want to retrieve, such as an <code>Integer</code> type.</p><p>We can do the
same for some of the other operations, such as
<code>SelectList</code>:<plain-text-body>{snippet:id
=e1|lang=java|url=camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisSelectListTest.java}</plain-text-body>And
the same for <code>UPDATE</code>, where we can send an <code>Account</code>
object as the IN body to
MyBatis:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateTest.java}</plain-text-body></p><h4
id="MyBatis-UsingInsertListStatementType">Using InsertList
StatementType</h4><p><strong>Available as of Camel 2.10</strong></p><p>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:<plain-text-body>{snippet:id=insertList|lang=xml|url=camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml}</plain-text-body>Then
you can insert multiple rows, by sending a Camel message to the <code
>mybatis</code> endpoint which uses the <code>InsertList</code> statement
>type, as shown
>below:<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java}</plain-text-body></p><h4
> id="MyBatis-UsingUpdateListStatementType">Using UpdateList
>StatementType</h4><p><strong>Available as of Camel
>2.11</strong></p><p>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:</p><parameter
>ac:name="">xml</parameter><plain-text-body><update id="batchUpdateAccount"
>parameterType="java.util.Map">
update ACCOUNT set
ACC_EMAIL = #{emailAddress}
where
ACC_ID in
- <foreach item="Account" collection="list"
open="(" close=")" separator=",">
+ <foreach item="Account" collection="list" open="(" close=")"
separator=",">
#{Account.id}
</foreach>
</update>
-]]></script>
-</div></div><p>Then you can update multiple rows, by sending a Camel message
to the mybatis endpoint which uses the UpdateList statement type, as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("direct:start")
- .to("mybatis:batchUpdateAccount?statementType=UpdateList")
- .to("mock:result");
-]]></script>
-</div></div><h4 id="MyBatis-UsingDeleteListStatementType">Using DeleteList
StatementType</h4><p><strong>Available as of Camel 2.11</strong></p><p>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:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[<delete
id="batchDeleteAccountById"
parameterType="java.util.List">
+</plain-text-body><p>Then you can update multiple rows, by sending a Camel
message to the mybatis endpoint which uses the UpdateList statement type, as
shown below:</p><plain-text-body>from("direct:start")
+ .to("mybatis:batchUpdateAccount?statementType=UpdateList")
+ .to("mock:result");
+</plain-text-body><h4 id="MyBatis-UsingDeleteListStatementType">Using
DeleteList StatementType</h4><p><strong>Available as of Camel
2.11</strong></p><p>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:</p><parameter
ac:name="">xml</parameter><plain-text-body><delete
id="batchDeleteAccountById" parameterType="java.util.List">
delete from ACCOUNT
where
ACC_ID in
- <foreach item="AccountID" collection="list"
open="(" close=")" separator=",">
+ <foreach item="AccountID" collection="list" open="(" close=")"
separator=",">
#{AccountID}
</foreach>
</delete>
-]]></script>
-</div></div><p>Then you can delete multiple rows, by sending a Camel message
to the mybatis endpoint which uses the DeleteList statement type, as shown
below:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("direct:start")
- .to("mybatis:batchDeleteAccount?statementType=DeleteList")
- .to("mock:result");
-]]></script>
-</div></div><h4
id="MyBatis-NoticeonInsertList,UpdateListandDeleteListStatementTypes">Notice on
InsertList, UpdateList and DeleteList StatementTypes</h4><p>Parameter of any
type (List, Map, etc.) can be passed to mybatis and an end user is responsible
for handling it as required<br clear="none"> with the help of <a shape="rect"
class="external-link" href="http://www.mybatis.org/core/dynamic-sql.html"
rel="nofollow">mybatis dynamic queries</a> capabilities.</p><h4
id="MyBatis-Scheduledpollingexample">Scheduled polling example</h4><p>This
component supports scheduled polling and can therefore be used as a <a
shape="rect" href="polling-consumer.html">Polling Consumer</a>. For example to
poll the database every minute:</p><div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("mybatis:selectAllAccounts?delay=60000").to("activemq:queue:allAccounts");]]></script>
-</div></div><p>See "ScheduledPollConsumer Options" on <a shape="rect"
href="polling-consumer.html">Polling Consumer</a> for more
options.</p><p>Alternatively you can use another mechanism for triggering the
scheduled polls, such as the <a shape="rect" href="timer.html">Timer</a> or <a
shape="rect" href="quartz.html">Quartz</a> components. <span
style="line-height: 1.4285715;">In the sample below we poll the database, every
30 seconds using the </span><a shape="rect" href="timer.html">Timer</a><span
style="line-height: 1.4285715;"> component and send the data to the JMS
queue:</span></p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[from("timer://pollTheDatabase?delay=30000").to("mybatis:selectAllAccounts").to("activemq:queue:allAccounts");
-]]></script>
-</div></div><p>And the MyBatis SQL mapping file used:</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <!-- Select with no parameters using the
result map for Account class. -->
- <select id="selectAllAccounts"
resultMap="AccountResult">
+</plain-text-body><p>Then you can delete multiple rows, by sending a Camel
message to the mybatis endpoint which uses the DeleteList statement type, as
shown below:</p><plain-text-body>from("direct:start")
+ .to("mybatis:batchDeleteAccount?statementType=DeleteList")
+ .to("mock:result");
+</plain-text-body><h4
id="MyBatis-NoticeonInsertList,UpdateListandDeleteListStatementTypes">Notice on
InsertList, UpdateList and DeleteList StatementTypes</h4><p>Parameter of any
type (List, Map, etc.) can be passed to mybatis and an end user is responsible
for handling it as required<br clear="none"> with the help of <a shape="rect"
class="external-link" href="http://www.mybatis.org/core/dynamic-sql.html"
rel="nofollow">mybatis dynamic queries</a> capabilities.</p><h4
id="MyBatis-Scheduledpollingexample">Scheduled polling example</h4><p>This
component supports scheduled polling and can therefore be used as a <a
shape="rect" href="polling-consumer.html">Polling Consumer</a>. For example to
poll the database every
minute:</p><plain-text-body>from("mybatis:selectAllAccounts?delay=60000").to("activemq:queue:allAccounts");</plain-text-body><p>See
"ScheduledPollConsumer Options" on <a shape="rect"
href="polling-consumer.html">Polling Consumer</a> for more
options.</p><p>Alterna
tively you can use another mechanism for triggering the scheduled polls, such
as the <a shape="rect" href="timer.html">Timer</a> or <a shape="rect"
href="quartz.html">Quartz</a> components. <span style="line-height:
1.4285715;">In the sample below we poll the database, every 30 seconds using
the </span><a shape="rect" href="timer.html">Timer</a><span style="line-height:
1.4285715;"> component and send the data to the JMS queue:</span></p><parameter
ac:name="">java</parameter><plain-text-body>from("timer://pollTheDatabase?delay=30000").to("mybatis:selectAllAccounts").to("activemq:queue:allAccounts");
+</plain-text-body><p>And the MyBatis SQL mapping file used:</p><parameter
ac:name="">xml</parameter><plain-text-body> <!-- Select with no parameters
using the result map for Account class. -->
+ <select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
-]]></script>
-</div></div><h4 id="MyBatis-UsingonConsume">Using onConsume</h4><p>This
component supports executing statements <strong>after</strong> data have been
consumed and processed by Camel. This allows you to do post updates in the
database. Notice all statements must be <code>UPDATE</code> statements. Camel
supports executing multiple statements whose names should be separated by
commas.</p><p>The route below illustrates we execute the
<strong>consumeAccount</strong> 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 pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: java; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-from("mybatis:selectUnprocessedAccounts?consumer.onConsume=consumeAccount").to("mock:results");
-]]></script>
-</div></div>And the statements in the sqlmap file:<div class="code panel pdl"
style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<select id="selectUnprocessedAccounts"
resultMap="AccountResult">
- select * from ACCOUNT where PROCESSED = false
-</select>
-]]></script>
-</div></div><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[
-<update id="consumeAccount" parameterType="Account">
- update ACCOUNT set PROCESSED = true where ACC_ID = #{id}
-</update>
-]]></script>
-</div></div><h4 id="MyBatis-Participatingintransactions"><span
style="line-height: 1.5;">Participating in transactions</span></h4><p>Setting
up a transaction manager under camel-mybatis can be a little bit fiddly, as it
involves externalising the database configuration outside the standard MyBatis
<code>SqlMapConfig.xml</code> file.</p><p>The first part requires the setup of
a <code>DataSource</code>. 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 <code>DataSource</code> to participate in Spring transactions (the
MyBatis <code>SqlSessionFactory</code> does just this).</p><div class="code
panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
+</plain-text-body><h4 id="MyBatis-UsingonConsume">Using onConsume</h4><p>This
component supports executing statements <strong>after</strong> data have been
consumed and processed by Camel. This allows you to do post updates in the
database. Notice all statements must be <code>UPDATE</code> statements. Camel
supports executing multiple statements whose names should be separated by
commas.</p><p>The route below illustrates we execute the
<strong>consumeAccount</strong> 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.<plain-text-body>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisQueueTest.java}</plain-text-body>And
the statements in the sqlmap
file:<plain-text-body>{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml}</plain-text-body><pla
in-text-body>{snippet:id=e2|lang=xml|url=camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml}</plain-text-body></p><h4
id="MyBatis-Participatingintransactions"><span style="line-height:
1.5;">Participating in transactions</span></h4><p>Setting up a transaction
manager under camel-mybatis can be a little bit fiddly, as it involves
externalising the database configuration outside the standard MyBatis
<code>SqlMapConfig.xml</code> file.</p><p>The first part requires the setup of
a <code>DataSource</code>. 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 <code>DataSource</code> to participate in Spring transactions (the
MyBatis <code>SqlSessionFactory</code> does just this).</p><parameter
ac:name="">xml</parameter><plain-text-body> <bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
- <bean
class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass"
value="org.postgresql.Driver"/>
- <property name="jdbcUrl"
value="jdbc:postgresql://localhost:5432/myDatabase"/>
- <property name="user"
value="myUser"/>
- <property name="password"
value="myPassword"/>
+ <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
+ <property name="driverClass"
value="org.postgresql.Driver"/>
+ <property name="jdbcUrl"
value="jdbc:postgresql://localhost:5432/myDatabase"/>
+ <property name="user" value="myUser"/>
+ <property name="password" value="myPassword"/>
</bean>
</constructor-arg>
</bean>
-]]></script>
-</div></div><p>This has the additional benefit of enabling the database
configuration to be externalised using property placeholders.</p><p>A
transaction manager is then configured to manage the outermost
<code>DataSource</code>:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource"
ref="dataSource"/>
+</plain-text-body><p>This has the additional benefit of enabling the database
configuration to be externalised using property placeholders.</p><p>A
transaction manager is then configured to manage the outermost
<code>DataSource</code>:</p><parameter
ac:name="">xml</parameter><plain-text-body> <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+ <property name="dataSource" ref="dataSource"/>
</bean>
-]]></script>
-</div></div><p>A <a shape="rect" class="external-link"
href="http://www.mybatis.org/spring/index.html"
rel="nofollow">mybatis-spring</a> <a shape="rect" class="external-link"
href="http://www.mybatis.org/spring/factorybean.html"
rel="nofollow"><code>SqlSessionFactoryBean</code></a> then wraps that same
<code>DataSource</code>:</p><div class="code panel pdl" style="border-width:
1px;"><div class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource"
ref="dataSource"/>
+</plain-text-body><p>A <a shape="rect" class="external-link"
href="http://www.mybatis.org/spring/index.html"
rel="nofollow">mybatis-spring</a> <a shape="rect" class="external-link"
href="http://www.mybatis.org/spring/factorybean.html"
rel="nofollow"><code>SqlSessionFactoryBean</code></a> then wraps that same
<code>DataSource</code>:</p><parameter
ac:name="">xml</parameter><plain-text-body> <bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
+ <property name="dataSource" ref="dataSource"/>
<!-- standard mybatis config file -->
- <property name="configLocation"
value="/META-INF/SqlMapConfig.xml"/>
+ <property name="configLocation"
value="/META-INF/SqlMapConfig.xml"/>
<!-- externalised mappers -->
- <property name="mapperLocations"
value="classpath*:META-INF/mappers/**/*.xml"/>
+ <property name="mapperLocations"
value="classpath*:META-INF/mappers/**/*.xml"/>
</bean>
-]]></script>
-</div></div><p>The camel-mybatis component is then configured with that
factory:</p><div class="code panel pdl" style="border-width: 1px;"><div
class="codeContent panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <bean id="mybatis"
class="org.apache.camel.component.mybatis.MyBatisComponent">
- <property name="sqlSessionFactory"
ref="sqlSessionFactory"/>
+</plain-text-body><p>The camel-mybatis component is then configured with that
factory:</p><parameter ac:name="">xml</parameter><plain-text-body> <bean
id="mybatis" class="org.apache.camel.component.mybatis.MyBatisComponent">
+ <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
-]]></script>
-</div></div><p>Finally, a <a shape="rect"
href="transactional-client.html">transaction policy</a> is defined over the top
of the transaction manager, which can then be used as usual:</p><div
class="code panel pdl" style="border-width: 1px;"><div class="codeContent
panelContent pdl">
-<script class="brush: xml; gutter: false; theme: Default"
type="syntaxhighlighter"><![CDATA[ <bean
id="PROPAGATION_REQUIRED"
class="org.apache.camel.spring.spi.SpringTransactionPolicy">
- <property name="transactionManager"
ref="txManager"/>
- <property name="propagationBehaviorName"
value="PROPAGATION_REQUIRED"/>
+</plain-text-body><p>Finally, a <a shape="rect"
href="transactional-client.html">transaction policy</a> is defined over the top
of the transaction manager, which can then be used as usual:</p><parameter
ac:name="">xml</parameter><plain-text-body> <bean
id="PROPAGATION_REQUIRED"
class="org.apache.camel.spring.spi.SpringTransactionPolicy">
+ <property name="transactionManager" ref="txManager"/>
+ <property name="propagationBehaviorName"
value="PROPAGATION_REQUIRED"/>
</bean>
- <camelContext id="my-model-context"
xmlns="http://camel.apache.org/schema/spring">
- <route id="insertModel">
- <from uri="direct:insert"/>
- <transacted ref="PROPAGATION_REQUIRED"/>
- <to
uri="mybatis:myModel.insert?statementType=Insert"/>
+ <camelContext id="my-model-context"
xmlns="http://camel.apache.org/schema/spring">
+ <route id="insertModel">
+ <from uri="direct:insert"/>
+ <transacted ref="PROPAGATION_REQUIRED"/>
+ <to uri="mybatis:myModel.insert?statementType=Insert"/>
</route>
</camelContext>
-]]></script>
-</div></div><p></p><h3 id="MyBatis-SeeAlso">See Also</h3>
-<ul><li><a shape="rect" href="configuring-camel.html">Configuring
Camel</a></li><li><a shape="rect"
href="component.html">Component</a></li><li><a shape="rect"
href="endpoint.html">Endpoint</a></li><li><a shape="rect"
href="getting-started.html">Getting Started</a></li></ul></div>
+</plain-text-body><p><parameter ac:name=""><a shape="rect"
href="endpoint-see-also.html">Endpoint See Also</a></parameter></p></div>
</td>
<td valign="top">
<div class="navigation">