Mybatis child mapper extending from parent
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 23 Apr 2015 01:42:46 +0000 (03:42 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 23 Apr 2015 01:42:46 +0000 (03:42 +0200)
MyBatis/MyBatis-Spring/pom.xml
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ChildMapper.java [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/ParentMapper.java [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleInheritanceService.java [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ChildMapper.xml [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/ParentMapper.xml [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml

index 0471644..dfa59f9 100644 (file)
@@ -21,7 +21,7 @@
     <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis</artifactId>
-      <version>3.2.2</version>
+      <version>3.2.8</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
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 (file)
index 0000000..4e5cbba
--- /dev/null
@@ -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<Ad> 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 (file)
index 0000000..5867f99
--- /dev/null
@@ -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<Ad> selectAdsParent();
+}
index 2f894af..13ebde1 100644 (file)
@@ -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 (file)
index 0000000..55b42d4
--- /dev/null
@@ -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<Ad> 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<Ad> 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 (file)
index 0000000..18d0550
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="de.example.mybatis.repository.mapper.ChildMapper">
+
+       <select id="selectAdsChild" resultType="de.example.mybatis.model.Ad">
+               select * FROM ad
+       </select>
+       
+</mapper>
\ 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 (file)
index 0000000..9490963
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="de.example.mybatis.repository.mapper.ParentMapper">
+
+       <select id="selectAdsParent" resultType="de.example.mybatis.model.Ad">
+               select * FROM ad
+       </select>
+       
+</mapper>
\ No newline at end of file
index 4670947..cc137b3 100644 (file)
         <property name="adMapper" ref="adMapper" />
         <property name="dataSource" ref="dataSource" />
     </bean>
+    
+    
+    <!-- 
+    MyBatis mappers inheritance
+    -->
+    
+    <bean id="exampleInheritanceService" class="de.example.mybatis.spring.service.ExampleInheritanceService">
+        <property name="childMapper" ref="childMapper" />
+        <property name="parentMapper" ref="parentMapper" />
+    </bean>
         
 </beans>