iBATISPage edited by willem jiangiBATISThe ibatis: component allows you to query, poll, insert, update and delete data in a relational database using Apache iBATIS. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ibatis</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
Message HeadersCamel will populate the result message, either IN or OUT with a header with the operationName used:
Message BodyCamel 1.6.1: The response from iBatis will be set as OUT body SamplesFor example if you wish to consume beans from a JMS queue and insert them into a database you could do the following: from("activemq:queue:newAccount"). to("ibatis:insertAccount?statementType=Insert"); Notice we have to specify the statementType, as we need to instruct Camel which SqlMapClient operation to invoke. Where insertAccount is the iBatis ID in the SQL map file: <!-- Insert example, using the Account parameter class --> <insert id="insertAccount" parameterClass="Account"> insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL ) values ( #id#, #firstName#, #lastName#, #emailAddress# ) </insert> Using StatementType for better control of IBatisAvailable as of Camel 1.6.1/2.0 from("direct:start") .to("ibatis:selectAccountById?statementType=QueryForObject") .to("mock:result"); In the code above we can invoke the iBatis statement selectAccountById and the IN body should contain the account id we want to retrieve, such as an Integer type. We can do the same for some of the other operations, such as QueryForList: from("direct:start") .to("ibatis:selectAllAccounts?statementType=QueryForList") .to("mock:result"); And the same for UPDATE, where we can send an Account object as IN body to iBatis: from("direct:start") .to("ibatis:updateAccount?statementType=Update") .to("mock:result"); Scheduled polling exampleSince this component does not support scheduled polling, you need to use another mechanism for triggering the scheduled polls, such as the Timer or Quartz components. In the sample below we poll the database, every 30 seconds using the Timer component and send the data to the JMS queue:
from("timer://pollTheDatabase?delay=30000").to("ibatis:selectAllAccounts?statementType=QueryForList").to("activemq:queue:allAccounts");
And the iBatis SQL map file used: <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllAccounts" resultMap="AccountResult"> select * from ACCOUNT </select> Using onConsumeThis component supports executing statements after data have been consumed and processed by Camel. This allows you to do post updates in the database. Notice all statements must be UPDATE statements. Camel supports executing multiple statements whose name should be separated by comma. The route below illustrates we execute the consumeAccount 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. from("ibatis:selectUnprocessedAccounts?consumer.>).to("mock:results");
And the statements in the sqlmap file: <select id="selectUnprocessedAccounts" resultMap="AccountResult"> select * from ACCOUNT where PROCESSED = false </select> <update id="consumeAccount" parameterClass="Account"> update ACCOUNT set PROCESSED = true where ACC_ID = #id# </update> See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > iBATIS confluence
- [CONF] Apache Camel > iBATIS confluence
- [CONF] Apache Camel > iBATIS confluence