Method 1 (only for insert) :
To add multiple record into "product" table
The XML file is as follows
<insert id="insertList" parameterType="HashMap" useGeneratedKeys="true">
INSERT INTO product (vendor_id, name ,description)
VALUES
<foreach collection="paramList" item="param" separator=",">
(#{param.vendorId}, #{param.name}, #{param.description})
</foreach>
</insert>
The invoking Java code is as follows
List products = new ArrayList();
HashMap<String, Object> para = new HashMap<String, Object>();
Product product1 = new Product("vendor_id1,"pen","good pen");
Proudct product2 = new Product("vendor_id1,"book","good book");
products.add(product1);
products.add(product2);
para.put ("paramList",products);
Integer status = mapper.insertList(para);
Method 2: (work both for insert and update)
You also can use ExecutorType.BATCH session in MyBatis to do batch insert and update
//batch session
session = sessionFactory.openSession(ExecutorType.BATCH, false);
Reference:
http://www.cnblogs.com/xcch/articles/2042298.html
To add multiple record into "product" table
The XML file is as follows
<insert id="insertList" parameterType="HashMap" useGeneratedKeys="true">
INSERT INTO product (vendor_id, name ,description)
VALUES
<foreach collection="paramList" item="param" separator=",">
(#{param.vendorId}, #{param.name}, #{param.description})
</foreach>
</insert>
The invoking Java code is as follows
List products = new ArrayList();
HashMap<String, Object> para = new HashMap<String, Object>();
Product product1 = new Product("vendor_id1,"pen","good pen");
Proudct product2 = new Product("vendor_id1,"book","good book");
products.add(product1);
products.add(product2);
para.put ("paramList",products);
Integer status = mapper.insertList(para);
Method 2: (work both for insert and update)
You also can use ExecutorType.BATCH session in MyBatis to do batch insert and update
//batch session
session = sessionFactory.openSession(ExecutorType.BATCH, false);
Reference:
http://www.cnblogs.com/xcch/articles/2042298.html
This did not work for me.
ReplyDeleteI kept getting this error
Return value ({paramList={paramList=[100, 200]}, dbType=hsql}) was not iterable.
at org.apache.ibatis.builder.xml.dynamic.ExpressionEvaluator.evaluateIterable(ExpressionEvaluator.java:45)
at org.apache.ibatis.builder.xml.dynamic.ForEachSqlNode.apply(ForEachSqlNode.java:36)
at org.apache.ibatis.builder.xml.dynamic.MixedSqlNode.apply(MixedSqlNode.java:14)
at org.apache.ibatis.builder.xml.dynamic.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:22)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:198)
at org.apache.ibatis.executor.statement.BaseStatementHandler.(BaseStatementHandler.java:45)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.(PreparedStatementHandler.java:1
It should work. You may set the wrong parameters
ReplyDeleteCan you please explain more for update, for multiple rows from list ?
ReplyDelete