You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Aaron Daubman <da...@gmail.com> on 2012/04/05 22:32:41 UTC
MyBatis batch-insert example?
Greetings,
I've found some similar questions asked in the past but there never seem to
be followups posted. I am looking for an example as how to perform batch
insert operations using MyBatis with Camel.
The desire is to be able to most efficiently insert 1000s of records at a
time from a List<beanObjet>.
My route currently returns such a list - it would be great to pass it right
along to:
to("mybatis:insertbeanObject?statementType=Insert");
This should be really simple - example SQL would be:
---snip---
INSERT INTO example
(example_id, name, value, other_value)
VALUES
(100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2'),
(102, 'Name 3', 'Value 3', 'Other 3'),
(103, 'Name 4', 'Value 4', 'Other 4');
---snip---
Then there is this example snippet as how to configure the mapper xml:
http://code.google.com/p/mybatis/issues/detail?id=537
I don't see why this wouldn't work (and am working to attempt it), but
would find it very useful to see a working example if one exists.
There is this mainly unanswered thread:
http://mybatis-user.963551.n3.nabble.com/MyBatis-Batch-Executor-td3784939.html
Then there is this 'working on something' teaser:
http://camel.465427.n5.nabble.com/Alan-batch-database-insert-td4452866.html#a4454264
I am hoping somebody out there has a full example they may be able to share?
Thanks!
Aaron
Re: MyBatis batch-insert example?
Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Apr 6, 2012 at 6:44 AM, Aaron Daubman <da...@gmail.com> wrote:
> I just created an issue and submitted a patch for this:
> https://issues.apache.org/jira/browse/CAMEL-5143
>
Thanks for the contribution. Seems like a good improvement.
Do you know if the old Apache iBatis project supports a similar
functionality like this?
We do have a camel-ibatis component as well. But of course people
should favor using MyBatis as thats the project that is active.
>
> On Thu, Apr 5, 2012 at 11:31 PM, Aaron Daubman <da...@gmail.com> wrote:
>
>> Ugh - I think I found my problem.
>>
>> The camel-mybatis code will iterate over any list passed in and attempt to
>> insert each item individually, bypassing foreach support =(
>>
>> ---from camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
>> ---
>>
>> private void doInsert(Exchange exchange) throws Exception {
>> SqlSessionFactory client = endpoint.getSqlSessionFactory();
>> SqlSession session = client.openSession();
>> try {
>> Object result;
>> Object in = exchange.getIn().getBody();
>> if (in != null) {
>> // lets handle arrays or collections of objects* Iterator<?> iter = ObjectHelper.createIterator(in);
>> while (iter.hasNext()) {*
>> Object value = iter.next();
>> LOG.trace("Inserting: {} using statement: {}", value, statement);
>> result = session.insert(statement, value);
>> doProcessResult(exchange, result);
>> }
>> } else {
>> LOG.trace("Inserting using statement: {}", statement);
>> result = session.insert(statement);
>> doProcessResult(exchange, result);
>> }
>> } finally {
>> session.commit();
>> session.close();
>> }
>> }
>>
>> ---from camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
>> ---
>>
>> Would it be possible to add a new statementType = 'InsertList' (similar to
>> SelectOne versus SelectList) and add it to the switch statement further up
>> in the MyBatisProducer.java code?
>>
>> I believe it would just be removing the iterator related code above and
>> simply calling:
>>
>> result = session.insert(statement, in);
>>
>> I would be more than happy to contribute this (any reason this should not
>> be included?) but have no idea how? Is there a guide to contributing
>> changes or a process for obtaining committer rights?
>>
>> Thanks again,
>> Aaron
>>
>> On Thu, Apr 5, 2012 at 4:32 PM, Aaron Daubman <da...@gmail.com> wrote:
>>
>>> Greetings,
>>>
>>> I've found some similar questions asked in the past but there never seem
>>> to be followups posted. I am looking for an example as how to perform batch
>>> insert operations using MyBatis with Camel.
>>> The desire is to be able to most efficiently insert 1000s of records at a
>>> time from a List<beanObjet>.
>>>
>>> My route currently returns such a list - it would be great to pass it
>>> right along to:
>>>
>>> to("mybatis:insertbeanObject?statementType=Insert");
>>>
>>>
>>> This should be really simple - example SQL would be:
>>> ---snip---
>>> INSERT INTO example
>>> (example_id, name, value, other_value)
>>> VALUES
>>> (100, 'Name 1', 'Value 1', 'Other 1'),
>>> (101, 'Name 2', 'Value 2', 'Other 2'),
>>> (102, 'Name 3', 'Value 3', 'Other 3'),
>>> (103, 'Name 4', 'Value 4', 'Other 4');
>>> ---snip---
>>>
>>> Then there is this example snippet as how to configure the mapper xml:
>>> http://code.google.com/p/mybatis/issues/detail?id=537
>>>
>>>
>>> I don't see why this wouldn't work (and am working to attempt it), but
>>> would find it very useful to see a working example if one exists.
>>>
>>> There is this mainly unanswered thread:
>>>
>>> http://mybatis-user.963551.n3.nabble.com/MyBatis-Batch-Executor-td3784939.html
>>>
>>> Then there is this 'working on something' teaser:
>>>
>>> http://camel.465427.n5.nabble.com/Alan-batch-database-insert-td4452866.html#a4454264
>>>
>>> I am hoping somebody out there has a full example they may be able to
>>> share?
>>>
>>> Thanks!
>>> Aaron
>>>
>>
>>
--
Claus Ibsen
-----------------
CamelOne 2012 Conference, May 15-16, 2012: http://camelone.com
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Re: MyBatis batch-insert example?
Posted by Aaron Daubman <da...@gmail.com>.
I just created an issue and submitted a patch for this:
https://issues.apache.org/jira/browse/CAMEL-5143
On Thu, Apr 5, 2012 at 11:31 PM, Aaron Daubman <da...@gmail.com> wrote:
> Ugh - I think I found my problem.
>
> The camel-mybatis code will iterate over any list passed in and attempt to
> insert each item individually, bypassing foreach support =(
>
> ---from camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
> ---
>
> private void doInsert(Exchange exchange) throws Exception {
> SqlSessionFactory client = endpoint.getSqlSessionFactory();
> SqlSession session = client.openSession();
> try {
> Object result;
> Object in = exchange.getIn().getBody();
> if (in != null) {
> // lets handle arrays or collections of objects* Iterator<?> iter = ObjectHelper.createIterator(in);
> while (iter.hasNext()) {*
> Object value = iter.next();
> LOG.trace("Inserting: {} using statement: {}", value, statement);
> result = session.insert(statement, value);
> doProcessResult(exchange, result);
> }
> } else {
> LOG.trace("Inserting using statement: {}", statement);
> result = session.insert(statement);
> doProcessResult(exchange, result);
> }
> } finally {
> session.commit();
> session.close();
> }
> }
>
> ---from camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
> ---
>
> Would it be possible to add a new statementType = 'InsertList' (similar to
> SelectOne versus SelectList) and add it to the switch statement further up
> in the MyBatisProducer.java code?
>
> I believe it would just be removing the iterator related code above and
> simply calling:
>
> result = session.insert(statement, in);
>
> I would be more than happy to contribute this (any reason this should not
> be included?) but have no idea how? Is there a guide to contributing
> changes or a process for obtaining committer rights?
>
> Thanks again,
> Aaron
>
> On Thu, Apr 5, 2012 at 4:32 PM, Aaron Daubman <da...@gmail.com> wrote:
>
>> Greetings,
>>
>> I've found some similar questions asked in the past but there never seem
>> to be followups posted. I am looking for an example as how to perform batch
>> insert operations using MyBatis with Camel.
>> The desire is to be able to most efficiently insert 1000s of records at a
>> time from a List<beanObjet>.
>>
>> My route currently returns such a list - it would be great to pass it
>> right along to:
>>
>> to("mybatis:insertbeanObject?statementType=Insert");
>>
>>
>> This should be really simple - example SQL would be:
>> ---snip---
>> INSERT INTO example
>> (example_id, name, value, other_value)
>> VALUES
>> (100, 'Name 1', 'Value 1', 'Other 1'),
>> (101, 'Name 2', 'Value 2', 'Other 2'),
>> (102, 'Name 3', 'Value 3', 'Other 3'),
>> (103, 'Name 4', 'Value 4', 'Other 4');
>> ---snip---
>>
>> Then there is this example snippet as how to configure the mapper xml:
>> http://code.google.com/p/mybatis/issues/detail?id=537
>>
>>
>> I don't see why this wouldn't work (and am working to attempt it), but
>> would find it very useful to see a working example if one exists.
>>
>> There is this mainly unanswered thread:
>>
>> http://mybatis-user.963551.n3.nabble.com/MyBatis-Batch-Executor-td3784939.html
>>
>> Then there is this 'working on something' teaser:
>>
>> http://camel.465427.n5.nabble.com/Alan-batch-database-insert-td4452866.html#a4454264
>>
>> I am hoping somebody out there has a full example they may be able to
>> share?
>>
>> Thanks!
>> Aaron
>>
>
>
Re: MyBatis batch-insert example?
Posted by Aaron Daubman <da...@gmail.com>.
Ugh - I think I found my problem.
The camel-mybatis code will iterate over any list passed in and attempt to
insert each item individually, bypassing foreach support =(
---from camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
---
private void doInsert(Exchange exchange) throws Exception {
SqlSessionFactory client = endpoint.getSqlSessionFactory();
SqlSession session = client.openSession();
try {
Object result;
Object in = exchange.getIn().getBody();
if (in != null) {
// lets handle arrays or collections of objects*
Iterator<?> iter = ObjectHelper.createIterator(in);
while (iter.hasNext()) {*
Object value = iter.next();
LOG.trace("Inserting: {} using statement: {}",
value, statement);
result = session.insert(statement, value);
doProcessResult(exchange, result);
}
} else {
LOG.trace("Inserting using statement: {}", statement);
result = session.insert(statement);
doProcessResult(exchange, result);
}
} finally {
session.commit();
session.close();
}
}
---from camel/trunk/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
---
Would it be possible to add a new statementType = 'InsertList' (similar to
SelectOne versus SelectList) and add it to the switch statement further up
in the MyBatisProducer.java code?
I believe it would just be removing the iterator related code above and
simply calling:
result = session.insert(statement, in);
I would be more than happy to contribute this (any reason this should not
be included?) but have no idea how? Is there a guide to contributing
changes or a process for obtaining committer rights?
Thanks again,
Aaron
On Thu, Apr 5, 2012 at 4:32 PM, Aaron Daubman <da...@gmail.com> wrote:
> Greetings,
>
> I've found some similar questions asked in the past but there never seem
> to be followups posted. I am looking for an example as how to perform batch
> insert operations using MyBatis with Camel.
> The desire is to be able to most efficiently insert 1000s of records at a
> time from a List<beanObjet>.
>
> My route currently returns such a list - it would be great to pass it
> right along to:
>
> to("mybatis:insertbeanObject?statementType=Insert");
>
>
> This should be really simple - example SQL would be:
> ---snip---
> INSERT INTO example
> (example_id, name, value, other_value)
> VALUES
> (100, 'Name 1', 'Value 1', 'Other 1'),
> (101, 'Name 2', 'Value 2', 'Other 2'),
> (102, 'Name 3', 'Value 3', 'Other 3'),
> (103, 'Name 4', 'Value 4', 'Other 4');
> ---snip---
>
> Then there is this example snippet as how to configure the mapper xml:
> http://code.google.com/p/mybatis/issues/detail?id=537
>
>
> I don't see why this wouldn't work (and am working to attempt it), but
> would find it very useful to see a working example if one exists.
>
> There is this mainly unanswered thread:
>
> http://mybatis-user.963551.n3.nabble.com/MyBatis-Batch-Executor-td3784939.html
>
> Then there is this 'working on something' teaser:
>
> http://camel.465427.n5.nabble.com/Alan-batch-database-insert-td4452866.html#a4454264
>
> I am hoping somebody out there has a full example they may be able to
> share?
>
> Thanks!
> Aaron
>