From a0e60fd91b65d95d55dd99b2420c06912333387e Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Wed, 4 Mar 2015 11:24:41 +0100 Subject: [PATCH] Pseudo batch update with CASE --- .../mybatis/repository/mapper/AdCustomMapper.java | 2 ++ .../spring/service/ExampleCustomService.java | 3 ++- .../mybatis/repository/mapper/AdCustomMapper.xml | 24 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) 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} + ) + + -- 2.1.4