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 2012/04/06 10:10:48 UTC

svn commit: r1310217 - in /camel/trunk/components/camel-mybatis/src: main/java/org/apache/camel/component/mybatis/ test/java/org/apache/camel/component/mybatis/ test/resources/org/apache/camel/component/mybatis/

Author: davsclaus
Date: Fri Apr  6 08:10:47 2012
New Revision: 1310217

URL: http://svn.apache.org/viewvc?rev=1310217&view=rev
Log:
CAMEL-5143: Added InsertList statement type to mybatis. Thanks to Aaron Daubman for the patch.

Added:
    camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java   (with props)
Modified:
    camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
    camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/StatementType.java
    camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml

Modified: camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java?rev=1310217&r1=1310216&r2=1310217&view=diff
==============================================================================
--- camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java (original)
+++ camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java Fri Apr  6 08:10:47 2012
@@ -29,7 +29,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * @version 
+ * @version
  */
 public class MyBatisProducer extends DefaultProducer {
 
@@ -51,6 +51,8 @@ public class MyBatisProducer extends Def
             doSelectList(exchange); break;
         case Insert:
             doInsert(exchange); break;
+        case InsertList:
+            doInsertList(exchange); break;
         case Update:
             doUpdate(exchange); break;
         case Delete:
@@ -126,6 +128,28 @@ public class MyBatisProducer extends Def
         }
     }
 
+    private void doInsertList(Exchange exchange) throws Exception {
+        SqlSessionFactory client = endpoint.getSqlSessionFactory();
+        SqlSession session = client.openSession();
+        try {
+            Object result;
+            Object in = exchange.getIn().getBody();
+            if (in != null) {
+                // just pass in the body as Object and allow MyBatis to iterate using its own foreach statement
+                LOG.trace("Inserting: {} using statement: {}", in, statement);
+                result = session.insert(statement, in);
+                doProcessResult(exchange, result);
+            } else {
+                LOG.trace("Inserting using statement: {}", statement);
+                result = session.insert(statement);
+                doProcessResult(exchange, result);
+            }
+        } finally {
+            session.commit();
+            session.close();
+        }
+    }
+
     private void doUpdate(Exchange exchange) throws Exception {
         SqlSessionFactory client = endpoint.getSqlSessionFactory();
         SqlSession session = client.openSession();

Modified: camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/StatementType.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/StatementType.java?rev=1310217&r1=1310216&r2=1310217&view=diff
==============================================================================
--- camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/StatementType.java (original)
+++ camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/StatementType.java Fri Apr  6 08:10:47 2012
@@ -19,9 +19,9 @@ package org.apache.camel.component.mybat
 /**
  * Statement types to instruct which MyBatis operation to use.
  *
- * @version 
+ * @version
  */
 public enum StatementType {
 
-    SelectOne, SelectList, Insert, Update, Delete
+    SelectOne, SelectList, Insert, InsertList, Update, Delete
 }

Added: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java?rev=1310217&view=auto
==============================================================================
--- camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java (added)
+++ camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java Fri Apr  6 08:10:47 2012
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mybatis;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class MyBatisInsertListTest extends MyBatisTestSupport {
+
+    @Test
+    public void testInsertList() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        Account account1 = new Account();
+        account1.setId(444);
+        account1.setFirstName("Willem");
+        account1.setLastName("Jiang");
+        account1.setEmailAddress("Faraway@gmail.com");
+
+        Account account2 = new Account();
+        account2.setId(555);
+        account2.setFirstName("Aaron");
+        account2.setLastName("Daubman");
+        account2.setEmailAddress("ReadTheDevList@gmail.com");
+
+        List<Account> accountList = new ArrayList<Account>(2);
+
+        accountList.add(account1);
+        accountList.add(account2);
+
+        // insert 2 new rows
+        template.sendBody("direct:start", accountList);
+
+        assertMockEndpointsSatisfied();
+
+        // there should be 4 rows now
+        Integer rows = template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class);
+        assertEquals("There should be 4 rows", 4, rows.intValue());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                from("direct:start")
+                    .to("mybatis:batchInsertAccount?statementType=InsertList")
+                    .to("mock:result");
+                // END SNIPPET: e1
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisInsertListTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml?rev=1310217&r1=1310216&r2=1310217&view=diff
==============================================================================
--- camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml (original)
+++ camel/trunk/components/camel-mybatis/src/test/resources/org/apache/camel/component/mybatis/Account.xml Fri Apr  6 08:10:47 2012
@@ -62,7 +62,24 @@ aliases to match the properties of the t
         )
     </insert>
 
-    <!-- Update example, using the Account parameter class -->
+    <!-- START SNIPPET: insertList -->
+    <!-- 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>
+    <!-- END SNIPPET: insertList -->
+
+  <!-- Update example, using the Account parameter class -->
     <update id="updateAccount" parameterType="Account">
         update ACCOUNT set
         ACC_FIRST_NAME = #{firstName},
@@ -97,4 +114,4 @@ aliases to match the properties of the t
         select count(*) from ACCOUNT
     </select>
 
-</mapper>
\ No newline at end of file
+</mapper>