MyBatis Example
Skip to end of metadata
Go to start of metadata

MyBatis Example

Available as of Camel 2.12

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

If you use maven then you can easily install the example from the command line:

About

This example shows how to exchange data using a shared database table.

The example has two Camel routes. The first route insert new data into the table, triggered by a timer to run every 5th second.
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.

Implementation

In the camel-mybatis.xml file in the src/main/resources/OSGI-INF/blueprint 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).

This example uses a bean to generate orders

And the CamelContext has two routes as shown below:

MyBatis SqlMapConfig.xml

MyBatis is configured using a SqlMapConfig.xml file located in the root of the classpath, eg in src/main/resources.
This configuration files setup MyBatis as well a pooled data source

MyBatis SqlMapConfig.xml

MyBatis mapping files

MyBatis allows to externalize the SQL queries and mapping from SQL to POJOs.

We have a plain POJO org.apache.camel.example.mybatis.Order which just has getter/setters as shown below:

Order POJO

And the MyBatis mapping file Order.xml is located in src/main/resources/org/apache/camel/example/mybatis where we map from SQL to this Order POJO, as shown below:

MyBatis mapping file for Order

Running the example

This example requires running in Apache Karaf / ServiceMix

To install Apache Camel in Karaf you type in the shell (we use version 2.12.0):

First you need to install the following features in Karaf/ServiceMix with:

Then you can install the Camel example:

And you can see the application running by tailing the logs

And you can use ctrl + c to stop tailing the log.

As of Camel 2.12.3 onwards

You can install and run this example using it's features.xml with the following 2 shell commands (substitute the ${version} placeholder below with the concrete version of Camel in use): 


See Also

Labels
  • No labels