Batch update (kind of) MyBatis
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 4 Mar 2015 02:30:16 +0000 (03:30 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 4 Mar 2015 02:30:16 +0000 (03:30 +0100)
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java
MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml
MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml

index 5b9c57b..f6c6bdf 100644 (file)
@@ -1,5 +1,6 @@
 package de.example.mybatis.repository.mapper;
 
+import java.util.List;
 import java.util.Set;
 
 import de.example.mybatis.mapper.filter.MyBatisScanFilter;
@@ -9,4 +10,8 @@ import de.example.mybatis.model.Ad;
 public interface AdCustomMapper extends MyBatisScanFilter {
 
        Set<Ad> selectAds();
+       
+       List<Ad> selectAdsList();
+       
+       void updateAdsBatch(List<Ad> ads);
 }
index 937660b..11e3677 100644 (file)
@@ -12,18 +12,20 @@ public class TestMain {
 
         logger.info("Starting application");
 
-        final ExampleService exampleService = (ExampleService) SpringContextLocator
-                .getInstance().getBean("exampleService");
-
-        exampleService.insertNewAd();
-
-        exampleService.getAdsByCriteria();
+//        final ExampleService exampleService = (ExampleService) SpringContextLocator
+//                .getInstance().getBean("exampleService");
+//
+//        exampleService.insertNewAd();
+//
+//        exampleService.getAdsByCriteria();
 
 
         final ExampleCustomService exampleCustomService = (ExampleCustomService) SpringContextLocator
                 .getInstance().getBean("exampleCustomService");
 
         exampleCustomService.getAds();
+        
+        exampleCustomService.updateAds();
     }
 
 }
index b2821fb..b69f69d 100644 (file)
@@ -1,6 +1,8 @@
 package de.example.mybatis.spring.service;
 
 import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.log4j.Logger;
@@ -40,4 +42,26 @@ public class ExampleCustomService {
             logger.info("\n");
         }
     }
+    
+    public void updateAds() {
+       logger.info("ExampleCustomService: updateAds");
+       
+       final List<Ad> ads = this.adCustomMapper.selectAdsList();
+       final List<Ad> updateAds = new ArrayList<Ad>();
+       
+       long companyId = 10;
+       long companyCategId = 20;
+       String mobileImage = "newimage.jpg";
+        for (final Ad ad : ads) {
+               ad.setCompanyCategId(companyCategId);
+               ad.setCompanyId(companyId);
+               ad.setAdMobileImage(mobileImage);
+               
+               updateAds.add(ad);
+               companyId++;
+               companyCategId++;
+        }
+        
+        this.adCustomMapper.updateAdsBatch(updateAds);
+    }
 }
index 0b762bd..a1464d2 100644 (file)
@@ -4,4 +4,24 @@
   <select id="selectAds" resultType="de.example.mybatis.model.Ad" flushCache="false" useCache="true" timeout="10000" statementType="PREPARED">
                select * FROM ad
   </select>
+  
+  <update id="updateAdsBatch" parameterType="List">
+               <!-- In order to make it work, the JDBC connection requires allowMultiQueries=true -->
+               <foreach collection="list" item="ad" separator=";">
+               UPDATE ad
+               <set>
+                               company_id=#{ad.companyId},
+                               company_categ_id=#{ad.companyCategId},
+                               ad_mobile_image=#{ad.adMobileImage}
+               </set>
+               <where>
+                               id=#{ad.id}
+               </where>
+               </foreach>
+  </update>
+  
+   <select id="selectAdsList" resultType="de.example.mybatis.model.Ad" flushCache="false" useCache="true" timeout="10000" statementType="PREPARED">
+               select * FROM ad
+  </select>
+
 </mapper>
\ No newline at end of file
index 145d172..2f91317 100644 (file)
@@ -42,7 +42,7 @@
         The JVM tries to find out if IPV6 is available by means of opening a random
         AF_INET6 POSIX socket.
          -->
-        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis_example?autoReconnect=true&amp;characterEncoding=UTF-8"/>
+        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis_example?allowMultiQueries=true&amp;autoReconnect=true&amp;characterEncoding=UTF-8"/>
         <property name="initialPoolSize" value="5"/>
         <property name="maxPoolSize" value="20"/>
         <property name="minPoolSize" value="10"/>