camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > MyBatis Example
Date Tue, 09 Jul 2013 07:46:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/MyBatis+Example">MyBatis
Example</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="MyBatisExample-MyBatisExample"></a>MyBatis Example</h2>
<p><b>Available as of Camel 2.12</b></p>

<p>This example is located in the <tt>examples/camel-example-mybatis</tt>
directory of the Camel distribution.<br/>
There is a <tt>README.txt</tt> file with instructions how to run it.</p>

<p>If you use maven then you can easily install the example from the command line:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
mvn install
</pre>
</div></div>

<h3><a name="MyBatisExample-About"></a>About</h3>

<p>This example shows how to exchange data using a shared database table.</p>

<p>The example has two Camel routes. The first route insert new data into the table,
triggered by a timer to run every 5th second.<br/>
The second route pickup the newly inserted rows from the table, process the row(s), and mark
the row(s) as processed when done; to avoid picking up the same rows again.</p>

<h3><a name="MyBatisExample-Implementation"></a>Implementation</h3>

<p>In the <tt>camel-mybatis.xml</tt> file in the <tt>src/main/resources/OSGI-INF/blueprint</tt>
folder we have the Blueprint XML file. This example uses an embedded Database so we use the
following bean to create and drop the table(s).</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
 &lt;bean id="initDatabase" class="org.apache.camel.example.mybatis.DatabaseBean"
        init-method="create" destroy-method="destroy"&gt;
    &lt;property name="camelContext" ref="myBatisAndCamel"/&gt;
  &lt;/bean&gt;
</pre>
</div></div>

<p>This example uses a bean to generate orders</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
 &lt;bean id="orderService" class="org.apache.camel.example.mybatis.OrderService"/&gt;
</pre>
</div></div>

<p>And the <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
has two routes as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  &lt;camelContext id="myBatisAndCamel" xmlns="http://camel.apache.org/schema/blueprint"&gt;

    &lt;!-- route that generate new orders and insert them in the database --&gt;
    &lt;route id="generateOrder-route"&gt;
      &lt;from uri="timer:foo?period=5s"/&gt;
      &lt;transform&gt;
        &lt;method ref="orderService" method="generateOrder"/&gt;
      &lt;/transform&gt;
      &lt;to uri="mybatis:insertOrder?statementType=Insert"/&gt;
      &lt;log message="Inserted new order ${body.id}"/&gt;
    &lt;/route&gt;

    &lt;!-- route that process the orders by picking up new rows from the database
         and when done processing then update the row to mark it as processed --&gt;
    &lt;route id="processOrder-route"&gt;
      &lt;from uri="mybatis:selectOrders?statementType=SelectList&amp;amp;consumer.onConsume=consumeOrder"/&gt;
      &lt;to uri="bean:orderService?method=processOrder"/&gt;
      &lt;log message="${body}"/&gt;
    &lt;/route&gt;

  &lt;/camelContext&gt;
</pre>
</div></div>

<h4><a name="MyBatisExample-MyBatisSqlMapConfig.xml"></a>MyBatis SqlMapConfig.xml</h4>
<p><a href="/confluence/display/CAMEL/MyBatis" title="MyBatis">MyBatis</a>
is configured using a <tt>SqlMapConfig.xml</tt> file located in the root of the
classpath, eg in <tt>src/main/resources</tt>.<br/>
This configuration files setup <a href="/confluence/display/CAMEL/MyBatis" title="MyBatis">MyBatis</a>
as well a pooled data source</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>MyBatis SqlMapConfig.xml</b></div><div
class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;

&lt;configuration&gt;

  &lt;settings&gt;
    &lt;setting name="useGeneratedKeys" value="false"/&gt;
  &lt;/settings&gt;

  &lt;!-- Use type aliases to avoid typing the full classname every time. --&gt;
  &lt;typeAliases&gt;
    &lt;typeAlias alias="Order" type="org.apache.camel.example.mybatis.Order"/&gt;
  &lt;/typeAliases&gt;

  &lt;!-- setup environment with JDBC data source --&gt;
  &lt;environments default="development"&gt;
    &lt;environment id="development"&gt;
      &lt;transactionManager type="JDBC"/&gt;
      &lt;dataSource type="POOLED"&gt;
        &lt;property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/&gt;
        &lt;property name="url" value="jdbc:derby:memory:mybatis;create=true"/&gt;
      &lt;/dataSource&gt;
    &lt;/environment&gt;
  &lt;/environments&gt;

  &lt;!-- mapping files --&gt;
  &lt;mappers&gt;
    &lt;mapper resource="org/apache/camel/example/mybatis/Order.xml"/&gt;
  &lt;/mappers&gt;

&lt;/configuration&gt;
</pre>
</div></div>

<h4><a name="MyBatisExample-MyBatismappingfiles"></a>MyBatis mapping files</h4>
<p><a href="/confluence/display/CAMEL/MyBatis" title="MyBatis">MyBatis</a>
allows to externalize the SQL queries and mapping from SQL to POJOs. </p>

<p>We have a plain POJO <tt>org.apache.camel.example.mybatis.Order</tt>
which just has getter/setters as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Order POJO</b></div><div class="codeContent
panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
public class Order {

    private int id;
    private String item;
    private int amount;
    private String description;
    private boolean processed;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isProcessed() {
        return processed;
    }

    public void setProcessed(boolean processed) {
        this.processed = processed;
    }
}
</pre>
</div></div>

<p>And the <a href="/confluence/display/CAMEL/MyBatis" title="MyBatis">MyBatis</a>
mapping file <tt>Order.xml</tt> is located in <tt>src/main/resources/org/apache/camel/example/mybatis</tt>
where we map from SQL to this Order POJO, as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>MyBatis mapping file for Order</b></div><div
class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;

&lt;mapper namespace="Order"&gt;

  &lt;!-- Result maps describe the mapping between the columns returned
 from a query, and the class properties.  A result map isn't
 necessary if the columns (or aliases) match to the properties
 exactly. --&gt;
  &lt;resultMap id="OrderResult" type="Order"&gt;
    &lt;result property="id" column="ORD_ID"/&gt;
    &lt;result property="item" column="ITEM"/&gt;
    &lt;result property="amount" column="ITEM_COUNT"/&gt;
    &lt;result property="description" column="ITEM_DESC"/&gt;
    &lt;result property="processed" column="ORD_DELETED"/&gt;
  &lt;/resultMap&gt;

  &lt;!-- Select with no parameters using the result map for Order class. --&gt;
  &lt;select id="selectOrders" resultMap="OrderResult"&gt;
    select * from ORDERS where ORD_DELETED = false order by ORD_ID
  &lt;/select&gt;

  &lt;!-- Insert example, using the Order parameter class --&gt;
  &lt;insert id="insertOrder" parameterType="Order"&gt;
    insert into ORDERS (
    ORD_ID,
    ITEM,
    ITEM_COUNT,
    ITEM_DESC,
    ORD_DELETED
    )
    values (
    #{id}, #{item}, #{amount}, #{description}, false
    )
  &lt;/insert&gt;

  &lt;update id="consumeOrder" parameterType="Order"&gt;
    update ORDERS set ORD_DELETED = true where ORD_ID = #{id}
  &lt;/update&gt;

&lt;/mapper&gt;
</pre>
</div></div>


<h3><a name="MyBatisExample-Runningtheexample"></a>Running the example</h3>

<p>This example requires running in Apache Karaf / ServiceMix</p>

<p>To install Apache Camel in Karaf you type in the shell (we use version 2.12.0):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  features:chooseurl camel 2.12.0
  features:install camel
</pre>
</div></div>

<p>First you need to install the following features in Karaf/ServiceMix with:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  features:install camel-mybatis
</pre>
</div></div>

<p>Then you can install the Camel example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  osgi:install -s mvn:org.apache.camel/camel-example-mybatis/2.12.0
</pre>
</div></div>

<p>And you can see the application running by tailing the logs</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  log:tail
</pre>
</div></div>

<p>And you can use ctrl + c to stop tailing the log.</p>



<h3><a name="MyBatisExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
	<li><a href="/confluence/display/CAMEL/MyBatis" title="MyBatis">MyBatis</a></li>
	<li><a href="/confluence/display/CAMEL/SQL+Example" title="SQL Example">SQL Example</a></li>
	<li><a href="/confluence/display/CAMEL/Hibernate+Example" title="Hibernate Example">Hibernate
Example</a></li>
</ul>

    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/MyBatis+Example">View
Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CAMEL/MyBatis+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message