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/11/19 11:18:01 UTC
svn commit: r1411117 - in /camel/trunk/components/camel-mybatis: ./
src/main/java/org/apache/camel/component/mybatis/
src/test/java/org/apache/camel/component/mybatis/
src/test/resources/org/apache/camel/component/mybatis/
Author: davsclaus
Date: Mon Nov 19 10:17:59 2012
New Revision: 1411117
URL: http://svn.apache.org/viewvc?rev=1411117&view=rev
Log:
CAMEL-5799: Added updateList, deleteList and executorType support to camel-mybatis. Thanks to Sergey Zhemzhitsky for the patch.
Added:
camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java (with props)
camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java (with props)
camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.java (with props)
Modified:
camel/trunk/components/camel-mybatis/ (props changed)
camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
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
Propchange: camel/trunk/components/camel-mybatis/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Nov 19 10:17:59 2012
@@ -14,3 +14,5 @@ eclipse-classes
*.ipr
*.iml
*.iws
+*.idea
+derby.log
Modified: camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java?rev=1411117&r1=1411116&r2=1411117&view=diff
==============================================================================
--- camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java (original)
+++ camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java Mon Nov 19 10:17:59 2012
@@ -24,6 +24,7 @@ import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultPollingEndpoint;
import org.apache.camel.util.ObjectHelper;
+import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
/**
@@ -34,6 +35,7 @@ public class MyBatisEndpoint extends Def
private MyBatisProcessingStrategy processingStrategy;
private String statement;
private StatementType statementType;
+ private ExecutorType executorType;
private int maxMessagesPerPoll;
public MyBatisEndpoint() {
@@ -87,6 +89,18 @@ public class MyBatisEndpoint extends Def
this.statementType = statementType;
}
+ public ExecutorType getExecutorType() {
+ return executorType;
+ }
+
+ public void setExecutorType(ExecutorType executorType) {
+ this.executorType = executorType;
+ }
+
+ public void setExecutorType(String executorType) {
+ this.executorType = ExecutorType.valueOf(executorType.toUpperCase());
+ }
+
public synchronized MyBatisProcessingStrategy getProcessingStrategy() {
if (processingStrategy == null) {
processingStrategy = new DefaultMyBatisProcessingStrategy();
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=1411117&r1=1411116&r2=1411117&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 Mon Nov 19 10:17:59 2012
@@ -23,6 +23,7 @@ import org.apache.camel.Message;
import org.apache.camel.impl.DefaultProducer;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
+import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,7 +44,15 @@ public class MyBatisProducer extends Def
}
public void process(Exchange exchange) throws Exception {
- SqlSession session = endpoint.getSqlSessionFactory().openSession();
+ SqlSession session;
+
+ ExecutorType executorType = endpoint.getExecutorType();
+ if (executorType == null) {
+ session = endpoint.getSqlSessionFactory().openSession();
+ } else {
+ session = endpoint.getSqlSessionFactory().openSession(executorType);
+ }
+
try {
switch (endpoint.getStatementType()) {
case SelectOne:
@@ -61,9 +70,15 @@ public class MyBatisProducer extends Def
case Update:
doUpdate(exchange, session);
break;
+ case UpdateList:
+ doUpdateList(exchange, session);
+ break;
case Delete:
doDelete(exchange, session);
break;
+ case DeleteList:
+ doDeleteList(exchange, session);
+ break;
default:
throw new IllegalArgumentException("Unsupported statementType: " + endpoint.getStatementType());
}
@@ -160,6 +175,21 @@ public class MyBatisProducer extends Def
}
}
+ private void doUpdateList(Exchange exchange, SqlSession session) throws Exception {
+ 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("Updating: {} using statement: {}", in, statement);
+ result = session.update(statement, in);
+ doProcessResult(exchange, result);
+ } else {
+ LOG.trace("Updating using statement: {}", statement);
+ result = session.update(statement);
+ doProcessResult(exchange, result);
+ }
+ }
+
private void doDelete(Exchange exchange, SqlSession session) throws Exception {
Object result;
Object in = exchange.getIn().getBody();
@@ -179,6 +209,21 @@ public class MyBatisProducer extends Def
}
}
+ private void doDeleteList(Exchange exchange, SqlSession session) throws Exception {
+ 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("Deleting: {} using statement: {}", in, statement);
+ result = session.delete(statement, in);
+ doProcessResult(exchange, result);
+ } else {
+ LOG.trace("Deleting using statement: {}", statement);
+ result = session.delete(statement);
+ doProcessResult(exchange, result);
+ }
+ }
+
private void doProcessResult(Exchange exchange, Object result) {
if (endpoint.getStatementType() == StatementType.SelectList || endpoint.getStatementType() == StatementType.SelectOne) {
Message answer = exchange.getIn();
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=1411117&r1=1411116&r2=1411117&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 Mon Nov 19 10:17:59 2012
@@ -23,5 +23,5 @@ package org.apache.camel.component.mybat
*/
public enum StatementType {
- SelectOne, SelectList, Insert, InsertList, Update, Delete
+ SelectOne, SelectList, Insert, InsertList, Update, UpdateList, Delete, DeleteList
}
Added: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java?rev=1411117&view=auto
==============================================================================
--- camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java (added)
+++ camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java Mon Nov 19 10:17:59 2012
@@ -0,0 +1,66 @@
+/**
+ * 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.Arrays;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class MyBatisDeleteListTest extends MyBatisTestSupport {
+
+ @Test
+ public void testDelete() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", Arrays.asList(123, 456));
+
+ assertMockEndpointsSatisfied();
+
+ // there should be 0 rows now
+ Integer rows = template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class);
+ assertEquals("There should be 0 rows", 0, rows.intValue());
+ }
+
+ @Test
+ public void testDeleteNotFound() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", Arrays.asList(999));
+
+ assertMockEndpointsSatisfied();
+
+ // there should be 2 rows now
+ Integer rows = template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class);
+ assertEquals("There should be 2 rows", 2, rows.intValue());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("mybatis:batchDeleteAccountById?statementType=DeleteList")
+ .to("mock:result");
+ }
+ };
+ }
+}
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisDeleteListTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java?rev=1411117&view=auto
==============================================================================
--- camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java (added)
+++ camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java Mon Nov 19 10:17:59 2012
@@ -0,0 +1,77 @@
+/**
+ * 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.Arrays;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class MyBatisExecutorTypeTest extends MyBatisTestSupport {
+
+ @Test
+ public void testUpdateBatch() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
+ Account account1 = new Account();
+ account1.setId(123);
+ account1.setFirstName("James");
+ account1.setLastName("Strachan");
+ account1.setEmailAddress("Other@gmail.com");
+
+ Account account2 = new Account();
+ account2.setId(456);
+ account2.setFirstName("Claus");
+ account2.setLastName("Ibsen");
+ account2.setEmailAddress("Other@gmail.com");
+
+ template.sendBody("direct:start", Arrays.asList(account1, account2));
+
+ assertMockEndpointsSatisfied();
+
+ // there should be 2 rows now
+ Integer rows = template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class);
+ assertEquals("There should be 2 rows", 2, rows.intValue());
+
+ Account james = template.requestBody("mybatis:selectAccountById?statementType=SelectOne", 123, Account.class);
+ assertEquals("James", james.getFirstName());
+ assertEquals("Strachan", james.getLastName());
+ assertEquals("Other@gmail.com", james.getEmailAddress());
+
+ Account claus = template.requestBody("mybatis:selectAccountById?statementType=SelectOne", 456, Account.class);
+ assertEquals("Claus", claus.getFirstName());
+ assertEquals("Ibsen", claus.getLastName());
+ assertEquals("Other@gmail.com", claus.getEmailAddress());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ from("direct:start")
+ .to("mybatis:updateAccount?statementType=Update&executorType=batch")
+ .to("mock:result");
+ // END SNIPPET: e1
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisExecutorTypeTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.java?rev=1411117&view=auto
==============================================================================
--- camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.java (added)
+++ camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.java Mon Nov 19 10:17:59 2012
@@ -0,0 +1,82 @@
+/**
+ * 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.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class MyBatisUpdateListTest extends MyBatisTestSupport {
+
+ @Test
+ public void testUpdateList() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+
+ Account account1 = new Account();
+ account1.setId(123);
+ account1.setFirstName("James");
+ account1.setLastName("Strachan");
+ account1.setEmailAddress("TryGuessing@gmail.com");
+
+ Account account2 = new Account();
+ account2.setId(456);
+ account2.setFirstName("Claus");
+ account2.setLastName("Ibsen");
+ account2.setEmailAddress("Noname@gmail.com");
+
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("list", Arrays.asList(account1, account2));
+ params.put("emailAddress", "Other@gmail.com");
+ template.sendBody("direct:start", params);
+
+ assertMockEndpointsSatisfied();
+
+ // there should be 2 rows now
+ Integer rows = template.requestBody("mybatis:count?statementType=SelectOne", null, Integer.class);
+ assertEquals("There should be 2 rows", 2, rows.intValue());
+
+ Account james = template.requestBody("mybatis:selectAccountById?statementType=SelectOne", 123, Account.class);
+ assertEquals("James", james.getFirstName());
+ assertEquals("Strachan", james.getLastName());
+ assertEquals("Other@gmail.com", james.getEmailAddress());
+
+ Account claus = template.requestBody("mybatis:selectAccountById?statementType=SelectOne", 456, Account.class);
+ assertEquals("Claus", claus.getFirstName());
+ assertEquals("Ibsen", claus.getLastName());
+ assertEquals("Other@gmail.com", claus.getEmailAddress());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ from("direct:start")
+ .to("mybatis:batchUpdateAccount?statementType=UpdateList")
+ .to("mock:result");
+ // END SNIPPET: e1
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisUpdateListTest.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=1411117&r1=1411116&r2=1411117&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 Mon Nov 19 10:17:59 2012
@@ -89,11 +89,30 @@ aliases to match the properties of the t
ACC_ID = #{id}
</update>
+ <update id="batchUpdateAccount" parameterType="java.util.Map">
+ update ACCOUNT set
+ ACC_EMAIL = #{emailAddress}
+ where
+ ACC_ID in
+ <foreach item="Account" collection="list" open="(" close=")" separator=",">
+ #{Account.id}
+ </foreach>
+ </update>
+
<!-- Delete example, using an integer as the parameter class -->
<delete id="deleteAccountById" parameterType="int">
delete from ACCOUNT where ACC_ID = #{id}
</delete>
+ <delete id="batchDeleteAccountById" parameterType="java.util.List">
+ delete from ACCOUNT
+ where
+ ACC_ID in
+ <foreach item="AccountID" collection="list" open="(" close=")" separator=",">
+ #{AccountID}
+ </foreach>
+ </delete>
+
<!-- START SNIPPET: e1 -->
<select id="selectUnprocessedAccounts" resultMap="AccountResult">
select * from ACCOUNT where PROCESSED = false