MyBatis Bean

Since Camel 2.22

Only producer is supported

The MyBatis Bean component allows you to query, insert, update and delete data in a relational database using MyBatis bean annotations.

This component can only be used as a producer. If you want to consume from MyBatis then use the regular mybatis component.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-mybatis</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

This component will by default load the MyBatis SqlMapConfig file from the root of the classpath with the expected name of SqlMapConfig.xml.
If the file is located in another location, you will need to configure the configurationUri option on the MyBatisComponent component.

Options

The MyBatis Bean component supports 4 options, which are listed below.

Name Description Default Type

configurationUri (producer)

Location of MyBatis xml configuration file. The default value is: SqlMapConfig.xml loaded from the classpath

SqlMapConfig.xml

String

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

basicPropertyBinding (advanced)

Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities

false

boolean

sqlSessionFactory (advanced)

To use the SqlSessionFactory

SqlSessionFactory

The MyBatis Bean endpoint is configured using URI syntax:

mybatis-bean:beanName:methodName

with the following path and query parameters:

Path Parameters (2 parameters):

Name Description Default Type

beanName

Required Name of the bean with the MyBatis annotations. This can either by a type alias or a FQN class name.

String

methodName

Required Name of the method on the bean that has the SQL query to be executed.

String

Query Parameters (6 parameters):

Name Description Default Type

executorType (producer)

The executor type to be used while executing statements. simple - executor does nothing special. reuse - executor reuses prepared statements. batch - executor reuses statements and batches updates. The value can be one of: SIMPLE, REUSE, BATCH

SIMPLE

ExecutorType

inputHeader (producer)

User the header value for input parameters instead of the message body. By default, inputHeader == null and the input parameters are taken from the message body. If outputHeader is set, the value is used and query parameters will be taken from the header instead of the body.

String

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

outputHeader (producer)

Store the query result in a header instead of the message body. By default, outputHeader == null and the query result is stored in the message body, any existing content in the message body is discarded. If outputHeader is set, the value is used as the name of the header to store the query result and the original message body is preserved. Setting outputHeader will also omit populating the default CamelMyBatisResult header since it would be the same as outputHeader all the time.

String

basicPropertyBinding (advanced)

Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities

false

boolean

synchronous (advanced)

Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported).

false

boolean

Message Headers

Camel will populate the result message, either IN or OUT with a header with the statement used:

Header Type Description

CamelMyBatisResult

Object

The response returned from MtBatis in any of the operations. For instance an INSERT could return the auto-generated key, or number of rows etc.

Message Body

The response from MyBatis will only be set as the body if it’s a SELECT statement. That means, for example, for INSERT statements Camel will not replace the body. This allows you to continue routing and keep the original body. The response from MyBatis is always stored in the header with the key CamelMyBatisResult.

Samples

For 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("mybatis-bean:AccountService:insertBeanAccount");

Notice we have to specify the bean name and method name, as we need to instruct Camel which kind of operation to invoke.

Where AccountService is the type alias for the bean that has the MyBatis bean annotations. You can configure type alias in the SqlMapConfig file:

    <typeAliases>
        <typeAlias alias="Account" type="org.apache.camel.component.mybatis.Account"/>
        <typeAlias alias="AccountService" type="org.apache.camel.component.mybatis.bean.AccountService"/>
    </typeAliases>
On the `AccountService` bean you can declare the MyBatis mappins using annotations as shown:
public interface AccountService {

    @Select("select ACC_ID as id, ACC_FIRST_NAME as firstName, ACC_LAST_NAME as lastName"
        + ", ACC_EMAIL as emailAddress from ACCOUNT where ACC_ID = #{id}")
    Account selectBeanAccountById(@Param("id") int no);

    @Select("select * from ACCOUNT order by ACC_ID")
    @ResultMap("Account.AccountResult")
    List<Account> selectBeanAllAccounts();

    @Insert("insert into ACCOUNT (ACC_ID,ACC_FIRST_NAME,ACC_LAST_NAME,ACC_EMAIL)"
        + " values (#{id}, #{firstName}, #{lastName}, #{emailAddress})")
    void insertBeanAccount(Account account);

}

Spring Boot Auto-Configuration

When using mybatis with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-mybatis-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

The component supports 11 options, which are listed below.

Name Description Default Type

camel.component.mybatis-bean.basic-property-binding

Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities

false

Boolean

camel.component.mybatis-bean.configuration-uri

Location of MyBatis xml configuration file. The default value is: SqlMapConfig.xml loaded from the classpath

SqlMapConfig.xml

String

camel.component.mybatis-bean.enabled

Whether to enable auto configuration of the mybatis-bean component. This is enabled by default.

Boolean

camel.component.mybatis-bean.lazy-start-producer

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

Boolean

camel.component.mybatis-bean.sql-session-factory

To use the SqlSessionFactory. The option is a org.apache.ibatis.session.SqlSessionFactory type.

String

camel.component.mybatis.basic-property-binding

Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities

false

Boolean

camel.component.mybatis.bridge-error-handler

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

Boolean

camel.component.mybatis.configuration-uri

Location of MyBatis xml configuration file. The default value is: SqlMapConfig.xml loaded from the classpath

SqlMapConfig.xml

String

camel.component.mybatis.enabled

Whether to enable auto configuration of the mybatis component. This is enabled by default.

Boolean

camel.component.mybatis.lazy-start-producer

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

Boolean

camel.component.mybatis.sql-session-factory

To use the SqlSessionFactory. The option is a org.apache.ibatis.session.SqlSessionFactory type.

String