From 9d9d2b88acb6b6b712b0ee76d87cb49ba64b7a8e Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 16 Jan 2015 08:14:54 +0100 Subject: [PATCH] MyBatis-Spring: my first custom mapper --- .../mybatis/repository/mapper/AdCustomMapper.java | 12 ++++++ .../java/de/example/mybatis/spring/TestMain.java | 7 ++++ .../spring/service/ExampleCustomService.java | 43 ++++++++++++++++++++++ .../mybatis/repository/mapper/AdCustomMapper.xml | 7 ++++ .../main/resources/generator/generatorConfig.xml | 36 +++++++++++++++--- .../src/main/resources/spring-config.xml | 4 ++ 6 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java create mode 100644 MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml 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 new file mode 100644 index 0000000..5b9c57b --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.java @@ -0,0 +1,12 @@ +package de.example.mybatis.repository.mapper; + +import java.util.Set; + +import de.example.mybatis.mapper.filter.MyBatisScanFilter; +import de.example.mybatis.model.Ad; + + +public interface AdCustomMapper extends MyBatisScanFilter { + + Set selectAds(); +} 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 396fbdf..937660b 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 @@ -2,6 +2,7 @@ package de.example.mybatis.spring; import org.apache.log4j.Logger; +import de.example.mybatis.spring.service.ExampleCustomService; import de.example.mybatis.spring.service.ExampleService; public class TestMain { @@ -17,6 +18,12 @@ public class TestMain { exampleService.insertNewAd(); exampleService.getAdsByCriteria(); + + + final ExampleCustomService exampleCustomService = (ExampleCustomService) SpringContextLocator + .getInstance().getBean("exampleCustomService"); + + exampleCustomService.getAds(); } } 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 new file mode 100644 index 0000000..b2821fb --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleCustomService.java @@ -0,0 +1,43 @@ +package de.example.mybatis.spring.service; + +import java.io.UnsupportedEncodingException; +import java.util.Set; + +import org.apache.log4j.Logger; + +import de.example.mybatis.model.Ad; +import de.example.mybatis.repository.mapper.AdCustomMapper; + + +public class ExampleCustomService { + private static final Logger logger = Logger.getLogger(ExampleService.class); + + private AdCustomMapper adCustomMapper; + + public void setAdCustomMapper(final AdCustomMapper adCustomMapper) { + this.adCustomMapper = adCustomMapper; + } + + public void getAds() { + logger.info("ExampleCustomService: getAds"); + + final Set ads = this.adCustomMapper.selectAds(); + + for (final Ad ad : ads) { + logger.info("Ad id: " + ad.getId()); + if (ad.getAdGps() != null) { + try { + logger.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8")); + } catch (final UnsupportedEncodingException e) { + logger.error("Encoding error", e); + } + } + logger.info("Ad mobileImage: " + ad.getAdMobileImage()); + logger.info("Ad companyCategId: " + ad.getCompanyCategId()); + logger.info("Ad companyId: " + ad.getCompanyId()); + logger.info("Ad createdAt: " + ad.getCreatedAt()); + logger.info("Ad updatedAt: " + ad.getUpdatedAt()); + logger.info("\n"); + } + } +} 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 new file mode 100644 index 0000000..0b762bd --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml b/MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml index 978cf3e..70ab2bc 100644 --- a/MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml +++ b/MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml @@ -21,6 +21,16 @@ + + + @@ -37,7 +47,7 @@ - + @@ -56,23 +66,39 @@ + -
+ --> + + + + + + + + +
diff --git a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml index 0fa5d05..d3ed00f 100644 --- a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml +++ b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml @@ -110,5 +110,9 @@ + + + + -- 2.1.4