Pseudo batch update with CASE
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Wed, 4 Mar 2015 10:24:41 +0000 (11:24 +0100)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Wed, 4 Mar 2015 10:24:41 +0000 (11:24 +0100)
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.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

index f6c6bdf..f73aa47 100644 (file)
@@ -14,4 +14,6 @@ public interface AdCustomMapper extends MyBatisScanFilter {
        List<Ad> selectAdsList();
        
        void updateAdsBatch(List<Ad> ads);
+       
+       void updateAdsBatchWithCase(List<Ad> ads);
 }
index b69f69d..6fbdef3 100644 (file)
@@ -49,7 +49,7 @@ public class ExampleCustomService {
        final List<Ad> ads = this.adCustomMapper.selectAdsList();
        final List<Ad> updateAds = new ArrayList<Ad>();
        
-       long companyId = 10;
+       long companyId = 66;
        long companyCategId = 20;
        String mobileImage = "newimage.jpg";
         for (final Ad ad : ads) {
@@ -62,6 +62,7 @@ public class ExampleCustomService {
                companyCategId++;
         }
         
+        this.adCustomMapper.updateAdsBatchWithCase(updateAds);
         this.adCustomMapper.updateAdsBatch(updateAds);
     }
 }
index a1464d2..b51bd4a 100644 (file)
                </foreach>
   </update>
   
+    <update id="updateAdsBatchWithCase" parameterType="List">          
+               UPDATE ad
+                       SET
+                       company_id = CASE id
+                               <foreach collection="list" item="ad">
+                               WHEN #{ad.id} THEN #{ad.companyId}                                      
+                               </foreach>
+                       END,
+                       company_categ_id = CASE id
+                               <foreach collection="list" item="ad">
+                               WHEN #{ad.id} THEN #{ad.companyCategId}                                         
+                               </foreach>
+                       END,
+                       ad_mobile_image = CASE id
+                               <foreach collection="list" item="ad">
+                               WHEN #{ad.id} THEN #{ad.adMobileImage}                                          
+                               </foreach>
+                       END
+               WHERE id IN (
+               <foreach collection="list" item="ad" separator=",">
+                       #{ad.id}                                        
+               </foreach> )
+  </update>
+  
    <select id="selectAdsList" resultType="de.example.mybatis.model.Ad" flushCache="false" useCache="true" timeout="10000" statementType="PREPARED">
                select * FROM ad
   </select>