Using StatementType for better control of MyBatis - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

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 SELECT, UPDATE, DELETE or INSERT etc. So for instance if we want to route to an MyBatis endpoint in which the IN body contains parameters to a SELECT statement we can do:

from("direct:start")
   .to("mybatis:selectAccountById?statementType=QueryForObject")
   .to("mock:result");

In the code above we invoke the MyBatis 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 SelectList :

from("direct:start")
   .to("mybatis:selectAllAccounts?statementType=SelectList")
   .to("mock:result");

And the same for UPDATE, where we can send an Account object as the IN body to MyBatis:

from("direct:start")
   .to("mybatis:updateAccount?statementType=Update")
   .to("mock:result");

Using onConsume

This 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 names should be separated by commas.

The route below illustrates executing the consumeAccount statement after the 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("mybatis:selectUnprocessedAccounts?consumer.
   onConsume=consumeAccount").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>