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 1cb54635595 CAMEL-18321: camel-mybatis - Should support using Map 
message body as-is for insert/update
1cb54635595 is described below

commit 1cb54635595de85f30e0df6c9a71a7c849abff83
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Jul 28 17:23:20 2022 +0200

    CAMEL-18321: camel-mybatis - Should support using Map message body as-is 
for insert/update
---
 .../camel/component/mybatis/MyBatisProducer.java   | 29 +++++++++++++++++-----
 .../camel/component/mybatis/MyBatisInsertTest.java | 23 +++++++++++++++++
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
index f3e34695f02..4e54d2689a2 100644
--- 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
+++ 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
@@ -16,7 +16,9 @@
  */
 package org.apache.camel.component.mybatis;
 
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -125,8 +127,13 @@ public class MyBatisProducer extends DefaultProducer {
         Object result;
         Object in = getInput(exchange);
         if (in != null) {
-            // lets handle arrays or collections of objects
-            Iterator<?> iter = ObjectHelper.createIterator(in);
+            Iterator<?> iter;
+            if (in instanceof Map) {
+                // we want the map as-is
+                iter = Collections.singletonList(in).iterator();
+            } else {
+                iter = ObjectHelper.createIterator(in);
+            }
             while (iter.hasNext()) {
                 Object value = iter.next();
                 LOG.trace("Inserting: {} using statement: {}", value, 
statement);
@@ -159,8 +166,13 @@ public class MyBatisProducer extends DefaultProducer {
         Object result;
         Object in = getInput(exchange);
         if (in != null) {
-            // lets handle arrays or collections of objects
-            Iterator<?> iter = ObjectHelper.createIterator(in);
+            Iterator<?> iter;
+            if (in instanceof Map) {
+                // we want the map as-is
+                iter = Collections.singletonList(in).iterator();
+            } else {
+                iter = ObjectHelper.createIterator(in);
+            }
             while (iter.hasNext()) {
                 Object value = iter.next();
                 LOG.trace("Updating: {} using statement: {}", value, 
statement);
@@ -193,8 +205,13 @@ public class MyBatisProducer extends DefaultProducer {
         Object result;
         Object in = getInput(exchange);
         if (in != null) {
-            // lets handle arrays or collections of objects
-            Iterator<?> iter = ObjectHelper.createIterator(in);
+            Iterator<?> iter;
+            if (in instanceof Map) {
+                // we want the map as-is
+                iter = Collections.singletonList(in).iterator();
+            } else {
+                iter = ObjectHelper.createIterator(in);
+            }
             while (iter.hasNext()) {
                 Object value = iter.next();
                 LOG.trace("Deleting: {} using statement: {}", value, 
statement);
diff --git 
a/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertTest.java
 
b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertTest.java
index 54d6afd8f14..02e16efa35d 100644
--- 
a/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertTest.java
+++ 
b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertTest.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.mybatis;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
@@ -44,6 +47,26 @@ public class MyBatisInsertTest extends MyBatisTestSupport {
         assertEquals(3, rows.intValue(), "There should be 3 rows");
     }
 
+    @Test
+    public void testInsertMap() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("id", 555);
+        map.put("firstName", "Donald");
+        map.put("lastName", "Duck");
+        map.put("emailAddress", "don...@duck.com");
+
+        template.sendBody("direct:start", map);
+
+        assertMockEndpointsSatisfied();
+
+        // there should be 3 rows now
+        Integer rows = 
template.requestBody("mybatis:count?statementType=SelectOne", null, 
Integer.class);
+        assertEquals(3, rows.intValue(), "There should be 3 rows");
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {

Reply via email to