From d3df2ce4aaaf938ec17b29fe46b91b99c21e846a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 4 Mar 2015 03:30:16 +0100 Subject: [PATCH] Batch update (kind of) MyBatis --- .../mybatis/repository/mapper/AdCustomMapper.java | 5 +++++ .../java/de/example/mybatis/spring/TestMain.java | 14 +++++++------ .../spring/service/ExampleCustomService.java | 24 ++++++++++++++++++++++ .../mybatis/repository/mapper/AdCustomMapper.xml | 20 ++++++++++++++++++ .../src/main/resources/spring-config.xml | 2 +- 5 files changed, 58 insertions(+), 7 deletions(-) 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 5b9c57b..f6c6bdf 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 @@ -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 selectAds(); + + List selectAdsList(); + + void updateAdsBatch(List ads); } diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java index 937660b..11e3677 100644 --- a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java @@ -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(); } } 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 b2821fb..b69f69d 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 @@ -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 ads = this.adCustomMapper.selectAdsList(); + final List updateAds = new ArrayList(); + + 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); + } } 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 0b762bd..a1464d2 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 @@ -4,4 +4,24 @@ + + + + + UPDATE ad + + company_id=#{ad.companyId}, + company_categ_id=#{ad.companyCategId}, + ad_mobile_image=#{ad.adMobileImage} + + + id=#{ad.id} + + + + + + \ No newline at end of file diff --git a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml index 145d172..2f91317 100644 --- a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml +++ b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml @@ -42,7 +42,7 @@ The JVM tries to find out if IPV6 is available by means of opening a random AF_INET6 POSIX socket. --> - + -- 2.1.4