| = MyBatis Bean Component |
| :doctitle: MyBatis Bean |
| :shortname: mybatis-bean |
| :artifactid: camel-mybatis |
| :description: Perform queries, inserts, updates or deletes in a relational database using MyBatis. |
| :since: 2.22 |
| :supportlevel: Stable |
| :tabs-sync-option: |
| :component-header: Only producer is supported |
| //Manually maintained attributes |
| :camel-spring-boot-name: mybatis |
| |
| *Since Camel {since}* |
| |
| *{component-header}* |
| |
| The MyBatis Bean component allows you to query, insert, update and |
| delete data in a relational database using http://mybatis.org/[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: |
| |
| [source,xml] |
| ---- |
| <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. |
| |
| |
| // component-configure options: START |
| |
| // component-configure options: END |
| |
| // component options: START |
| include::partial$component-configure-options.adoc[] |
| include::partial$component-endpoint-options.adoc[] |
| // component options: END |
| |
| // endpoint options: START |
| |
| // endpoint options: END |
| // component headers: START |
| include::partial$component-endpoint-headers.adoc[] |
| // component headers: END |
| |
| == 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: |
| |
| [source,java] |
| ---- |
| 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: |
| |
| [source,xml] |
| ---- |
| <typeAliases> |
| <typeAlias alias="Account" type="org.apache.camel.component.mybatis.Account"/> |
| <typeAlias alias="AccountService" type="org.apache.camel.component.mybatis.bean.AccountService"/> |
| </typeAliases> |
| ---- |
| [source] |
| |
| On the `AccountService` bean you can declare the MyBatis mappins using annotations as shown: |
| |
| [source,java] |
| ---- |
| 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); |
| |
| } |
| ---- |
| |
| |
| |
| include::spring-boot:partial$starter.adoc[] |