From: gu.martinm@gmail.com Date: Wed, 4 Mar 2015 10:24:41 +0000 (+0100) Subject: Pseudo batch update with CASE X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=a0e60fd91b65d95d55dd99b2420c06912333387e;p=JavaForFun Pseudo batch update with CASE --- diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java index f6c6bdf..f73aa47 100644 --- a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java @@ -14,4 +14,6 @@ public interface AdCustomMapper extends MyBatisScanFilter { List selectAdsList(); void updateAdsBatch(List ads); + + void updateAdsBatchWithCase(List ads); } diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java index b69f69d..6fbdef3 100644 --- a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java @@ -49,7 +49,7 @@ public class ExampleCustomService { final List ads = this.adCustomMapper.selectAdsList(); final List updateAds = new ArrayList(); - 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); } } diff --git a/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml index a1464d2..b51bd4a 100644 --- a/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml +++ b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml @@ -20,6 +20,30 @@ + + UPDATE ad + SET + company_id = CASE id + + WHEN #{ad.id} THEN #{ad.companyId} + + END, + company_categ_id = CASE id + + WHEN #{ad.id} THEN #{ad.companyCategId} + + END, + ad_mobile_image = CASE id + + WHEN #{ad.id} THEN #{ad.adMobileImage} + + END + WHERE id IN ( + + #{ad.id} + ) + +