You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/22 09:35:26 UTC
(camel) branch main updated: CAMEL-19746: Update mybatis doc
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ec9d3529786 CAMEL-19746: Update mybatis doc
ec9d3529786 is described below
commit ec9d3529786d32b6d4569849f260ca304169f13e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Dec 22 10:35:16 2023 +0100
CAMEL-19746: Update mybatis doc
---
.../src/main/docs/mybatis-component.adoc | 81 ++++++++++++++++++----
1 file changed, 67 insertions(+), 14 deletions(-)
diff --git a/components/camel-mybatis/src/main/docs/mybatis-component.adoc b/components/camel-mybatis/src/main/docs/mybatis-component.adoc
index 3c427081add..610fdd4b3f9 100644
--- a/components/camel-mybatis/src/main/docs/mybatis-component.adoc
+++ b/components/camel-mybatis/src/main/docs/mybatis-component.adoc
@@ -117,6 +117,13 @@ control so you can control whether the SQL statement to be executed is a
to route to an MyBatis endpoint in which the IN body contains parameters
to a `SELECT` statement we can do:
+[source,java]
+----
+from("direct:start")
+ .to("mybatis:selectAccountById?statementType=SelectOne")
+ .to("mock:result");
+----
+
In the code above we can invoke the MyBatis statement
`selectAccountById` and the IN body should contain the account id we
want to retrieve, such as an `Integer` type.
@@ -124,19 +131,58 @@ want to retrieve, such as an `Integer` type.
We can do the same for some of the other operations, such as
`SelectList`:
+[source,java]
+----
+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:
+[source,java]
+----
+from("direct:start")
+ .to("mybatis:updateAccount?statementType=Update")
+ .to("mock:result");
+----
+
=== Using InsertList StatementType
MyBatis allows you to insert multiple rows using its for-each batch
driver. To use this, you need to use the <foreach> in the mapper XML
file. For example as shown below:
+[source,xml]
+----
+<!-- Batch Insert example, using the Account parameter class -->
+<insert id="batchInsertAccount" parameterType="java.util.List">
+ insert into ACCOUNT (
+ ACC_ID,
+ ACC_FIRST_NAME,
+ ACC_LAST_NAME,
+ ACC_EMAIL
+ )
+ values (
+ <foreach item="Account" collection="list" open="" close="" separator="),(">
+ #{Account.id}, #{Account.firstName}, #{Account.lastName}, #{Account.emailAddress}
+ </foreach>
+ )
+</insert>
+----
+
Then you can insert multiple rows, by sending a Camel message to the
`mybatis` endpoint which uses the `InsertList` statement type, as shown
below:
+[source,java]
+----
+from("direct:start")
+ .to("mybatis:batchInsertAccount?statementType=InsertList")
+ .to("mock:result");
+----
+
=== Using UpdateList StatementType
MyBatis allows you to update multiple rows using its for-each batch
@@ -199,9 +245,8 @@ from("direct:start")
=== Notice on InsertList, UpdateList and DeleteList StatementTypes
Parameter of any type (List, Map, etc.) can be passed to mybatis and an
-end user is responsible for handling it as required +
- with the help of http://www.mybatis.org/mybatis-3/dynamic-sql.html[mybatis
-dynamic queries] capabilities.
+end user is responsible for handling it as required with the help of
+http://www.mybatis.org/mybatis-3/dynamic-sql.html[mybatis dynamic queries] capabilities.
=== Scheduled polling example
@@ -215,9 +260,6 @@ from("mybatis:selectAllAccounts?delay=60000")
.to("activemq:queue:allAccounts");
----
-See "ScheduledPollConsumer Options"
-on Polling Consumer for more options.
-
Alternatively you can use another mechanism for triggering the scheduled
polls, such as the xref:timer-component.adoc[Timer] or xref:timer-component.adoc[Quartz]
components. In the sample below we poll the database, every 30 seconds
@@ -253,8 +295,21 @@ 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.
+[source,java]
+----
+from("mybatis:selectUnprocessedAccounts?onConsume=consumeAccount")
+ .to("mock:results");
+----
+
And the statements in the sqlmap file:
+[source,xml]
+----
+<update id="consumeAccount" parameterType="Account">
+ update ACCOUNT set PROCESSED = true where ACC_ID = #{id}
+</update>
+----
+
=== Participating in transactions
Setting up a transaction manager under camel-mybatis can be a little bit
@@ -303,9 +358,9 @@ then wraps that same `DataSource`:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- standard mybatis config file -->
-<property name="configLocation" value="/META-INF/SqlMapConfig.xml"/>
+ <property name="configLocation" value="/META-INF/SqlMapConfig.xml"/>
<!-- externalised mappers -->
-<property name="mapperLocations" value="classpath*:META-INF/mappers/**/*.xml"/>
+ <property name="mapperLocations" value="classpath*:META-INF/mappers/**/*.xml"/>
</bean>
----
@@ -318,9 +373,7 @@ The camel-mybatis component is then configured with that factory:
</bean>
----
-Finally, a transaction policy is defined
-over the top of the transaction manager, which can then be used as
-usual:
+Finally, a transaction policy is defined over the top of the transaction manager, which can then be used as usual:
[source,xml]
----
@@ -347,16 +400,16 @@ Spring Boot users can use https://mybatis.org/spring-boot-starter/mybatis-spring
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
- <version>3.0.1</version>
+ <version>3.0.3</version>
</dependency>
----
-in particular AutoConfigured beans from mybatis-spring-boot-starter can be used as follow:
+in particular auto configured beans from mybatis-spring-boot-starter can be used as follows:
[source,properties]
----
#application.properties
-camel.component.mybatis.sql-session-factory=#sqlSessionFactory
+camel.component.mybatis.sql-session-factory = #sqlSessionFactory
----
include::spring-boot:partial$starter.adoc[]