From: Gustavo Martin Morcuende Date: Thu, 23 Apr 2015 01:42:46 +0000 (+0200) Subject: Mybatis child mapper extending from parent X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=82af2a18a7fef30e9de57ec0adcbfdbfbb95aff0;p=JavaForFun Mybatis child mapper extending from parent --- diff --git a/MyBatis/MyBatis-Spring/pom.xml b/MyBatis/MyBatis-Spring/pom.xml index 0471644..dfa59f9 100644 --- a/MyBatis/MyBatis-Spring/pom.xml +++ b/MyBatis/MyBatis-Spring/pom.xml @@ -21,7 +21,7 @@ org.mybatis mybatis - 3.2.2 + 3.2.8 org.springframework diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ChildMapper.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ChildMapper.java new file mode 100644 index 0000000..4e5cbba --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ChildMapper.java @@ -0,0 +1,11 @@ +package de.example.mybatis.repository.mapper; + +import java.util.List; + +import de.example.mybatis.model.Ad; + + +public interface ChildMapper extends ParentMapper { + + List selectAdsChild(); +} diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ParentMapper.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ParentMapper.java new file mode 100644 index 0000000..5867f99 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ParentMapper.java @@ -0,0 +1,12 @@ +package de.example.mybatis.repository.mapper; + +import java.util.List; + +import de.example.mybatis.mapper.filter.MyBatisScanFilter; +import de.example.mybatis.model.Ad; + + +public interface ParentMapper extends MyBatisScanFilter { + + List selectAdsParent(); +} 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 2f894af..13ebde1 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 @@ -8,6 +8,7 @@ import org.springframework.jdbc.CannotGetJdbcConnectionException; import de.example.mybatis.spring.service.BatchAndSimpleSameTrx; import de.example.mybatis.spring.service.ExampleBatchService; import de.example.mybatis.spring.service.ExampleCustomService; +import de.example.mybatis.spring.service.ExampleInheritanceService; import de.example.mybatis.spring.service.ExampleService; public class TestMain { @@ -41,17 +42,23 @@ public class TestMain { // exampleBatchService.insertBatchNewAd(); - final BatchAndSimpleSameTrx batchAndSimpleSameTrx = (BatchAndSimpleSameTrx) SpringContextLocator - .getInstance().getBean("batchAndSimpleSameTrx"); +// final BatchAndSimpleSameTrx batchAndSimpleSameTrx = (BatchAndSimpleSameTrx) SpringContextLocator +// .getInstance().getBean("batchAndSimpleSameTrx"); +// +// try { +// batchAndSimpleSameTrx.insertNewAd(); +// } catch (CannotGetJdbcConnectionException e) { +// logger.error("Error exception: ", e); +// } catch (SQLException e) { +// logger.error("Error exception: ", e); +// } - try { - batchAndSimpleSameTrx.insertNewAd(); - } catch (CannotGetJdbcConnectionException e) { - logger.error("Error exception: ", e); - } catch (SQLException e) { - logger.error("Error exception: ", e); - } + final ExampleInheritanceService exampleBatchService = (ExampleInheritanceService) SpringContextLocator + .getInstance().getBean("exampleInheritanceService"); + + exampleBatchService.selectAdsParent(); + exampleBatchService.selectAdsChild(); } } diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleInheritanceService.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleInheritanceService.java new file mode 100644 index 0000000..55b42d4 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleInheritanceService.java @@ -0,0 +1,72 @@ +package de.example.mybatis.spring.service; + +import java.io.UnsupportedEncodingException; +import java.util.List; + +import org.apache.log4j.Logger; + +import de.example.mybatis.model.Ad; +import de.example.mybatis.repository.mapper.ChildMapper; +import de.example.mybatis.repository.mapper.ParentMapper; + + +public class ExampleInheritanceService { + private static final Logger logger = Logger.getLogger(ExampleService.class); + + private ChildMapper childMapper; + private ParentMapper parentMapper; + + public void setChildMapper(final ChildMapper childMapper) { + this.childMapper = childMapper; + } + + public void setParentMapper(final ParentMapper parentMapper) { + this.parentMapper = parentMapper; + } + + public void selectAdsChild() { + logger.info("ExampleCustomService: getAds"); + + final List ads = this.childMapper.selectAdsChild(); + + 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"); + } + } + + public void selectAdsParent() { + logger.info("ExampleCustomService: updateAds"); + + final List ads = this.childMapper.selectAdsParent(); + + 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/ChildMapper.xml b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ChildMapper.xml new file mode 100644 index 0000000..18d0550 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ChildMapper.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ParentMapper.xml b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ParentMapper.xml new file mode 100644 index 0000000..9490963 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ParentMapper.xml @@ -0,0 +1,9 @@ + + + + + + + \ 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 4670947..cc137b3 100644 --- a/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml +++ b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml @@ -164,5 +164,15 @@ + + + + + + + +