Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
MyBatis ExampleAvailable as of Camel 2.12 This example is located in the If you use maven then you can easily install the example from the command line: mvn install AboutThis 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. ImplementationIn the <bean id="initDatabase" class="org.apache.camel.example.mybatis.DatabaseBean" init-method="create" destroy-method="destroy"> <property name="camelContext" ref="myBatisAndCamel"/> </bean> This example uses a bean to generate orders <bean id="orderService" class="org.apache.camel.example.mybatis.OrderService"/> And the CamelContext has two routes as shown below: <camelContext id="myBatisAndCamel" xmlns="http://camel.apache.org/schema/blueprint"> <!-- route that generate new orders and insert them in the database --> <route id="generateOrder-route"> <from uri="timer:foo?period=5s"/> <transform> <method ref="orderService" method="generateOrder"/> </transform> <to uri="mybatis:insertOrder?statementType=Insert"/> <log message="Inserted new order ${body.id}"/> </route> <!-- 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 --> <route id="processOrder-route"> <from uri="mybatis:selectOrders?statementType=SelectList&consumer.onConsume=consumeOrder"/> <to uri="bean:orderService?method=processOrder"/> <log message="${body}"/> </route> </camelContext> MyBatis SqlMapConfig.xmlMyBatis is configured using a MyBatis SqlMapConfig.xml <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="useGeneratedKeys" value="false"/> </settings> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAliases> <typeAlias alias="Order" type="org.apache.camel.example.mybatis.Order"/> </typeAliases> <!-- setup environment with JDBC data source --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/> <property name="url" value="jdbc:derby:memory:mybatis;create=true"/> </dataSource> </environment> </environments> <!-- mapping files --> <mappers> <mapper resource="org/apache/camel/example/mybatis/Order.xml"/> </mappers> </configuration> MyBatis mapping filesMyBatis allows to externalize the SQL queries and mapping from SQL to POJOs. We have a plain POJO Order POJO 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; } } And the MyBatis mapping file MyBatis mapping file for Order <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="Order"> <!-- 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. --> <resultMap id="OrderResult" type="Order"> <result property="id" column="ORD_ID"/> <result property="item" column="ITEM"/> <result property="amount" column="ITEM_COUNT"/> <result property="description" column="ITEM_DESC"/> <result property="processed" column="ORD_DELETED"/> </resultMap> <!-- Select with no parameters using the result map for Order class. --> <select id="selectOrders" resultMap="OrderResult"> select * from ORDERS where ORD_DELETED = false order by ORD_ID </select> <!-- Insert example, using the Order parameter class --> <insert id="insertOrder" parameterType="Order"> insert into ORDERS ( ORD_ID, ITEM, ITEM_COUNT, ITEM_DESC, ORD_DELETED ) values ( #{id}, #{item}, #{amount}, #{description}, false ) </insert> <update id="consumeOrder" parameterType="Order"> update ORDERS set ORD_DELETED = true where ORD_ID = #{id} </update> </mapper> Running the exampleThis example requires running in Apache Karaf / ServiceMix To install Apache Camel in Karaf you type in the shell (we use version 2.12.0): features:chooseurl camel 2.12.0 features:install camel First you need to install the following features in Karaf/ServiceMix with: features:install camel-mybatis Then you can install the Camel example: osgi:install -s mvn:org.apache.camel/camel-example-mybatis/2.12.0 And you can see the application running by tailing the logs log:tail 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:addUrl mvn:org.apache.camel/camel-example-mybatis/${version}/xml/features features:install camel-example-mybatis See Also |