MyBatis-Spring: my first custom mapper
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 16 Jan 2015 07:14:54 +0000 (08:14 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 16 Jan 2015 07:14:54 +0000 (08:14 +0100)
MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/repository/mapper/AdCustomMapper.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/ExampleCustomService.java [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/resources/de/example/mybatis/repository/mapper/AdCustomMapper.xml [new file with mode: 0644]
MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml
MyBatis/MyBatis-Spring/src/main/resources/spring-config.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 (file)
index 0000000..5b9c57b
--- /dev/null
@@ -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<Ad> selectAds();
+}
index 396fbdf..937660b 100644 (file)
@@ -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 (file)
index 0000000..b2821fb
--- /dev/null
@@ -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<Ad> 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 (file)
index 0000000..0b762bd
--- /dev/null
@@ -0,0 +1,7 @@
+<?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.AdCustomMapper" >
+  <select id="selectAds" resultType="de.example.mybatis.model.Ad" flushCache="false" useCache="true" timeout="10000" statementType="PREPARED">
+               select * FROM ad
+  </select>
+</mapper>
\ No newline at end of file
index 978cf3e..70ab2bc 100644 (file)
         <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin">
         </plugin>
 
+        <!-- Not very useful because you can not rely on its results (equals/hashcode depend on bussines rules)
+        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin">
+        </plugin>
+
+        <plugin type="org.mybatis.generator.plugins.SerializablePlugin">
+            <property name="suppressJavaInterface" value="true"/>
+        </plugin>
+        -->
+
+
         <commentGenerator>
             <property name="suppressAllComments" value="false" />
             <property name="suppressDate" value="false" />
@@ -37,7 +47,7 @@
         </jdbcConnection>
 
         <javaTypeResolver>
-            <property name="forceBigDecimals" value="false" />
+            <property name="forceBigDecimals" value="true" />
         </javaTypeResolver>
 
         <javaModelGenerator targetPackage="de.example.mybatis.model"  targetProject="MAVEN">
             <property name="enableSubPackages" value="true" />
         </javaClientGenerator>
 
+        <!--
+            There is no need of declaring every table in data base.
+
         <table schema="mybatis_example" tableName="ad" domainObjectName="Ad">
             <property name="useActualColumnNames" value="false" />
             <property name="ignoreQualifiersAtRuntime" value="true" />
-            <!--
             <mybatis:scan/> supports filtering the mappers created by either specifying a marker interface or an annotation
-            -->
             <property name="rootInterface" value="de.example.mybatis.mapper.filter.MyBatisScanFilter" />
             <generatedKey column="id" sqlStatement="MySql" identity="true" type="post" />
         </table>
         <table schema="mybatis_example" tableName="ad_description" domainObjectName="AdDescription">
             <property name="useActualColumnNames" value="false" />
             <property name="ignoreQualifiersAtRuntime" value="true" />
-            <!--
             <mybatis:scan/> supports filtering the mappers created by either specifying a marker interface or an annotation
-            -->
             <property name="rootInterface" value="de.example.mybatis.mapper.filter.MyBatisScanFilter" />
             <generatedKey column="id" sqlStatement="MySql" identity="true" type="post" />
         </table>
+        -->
+
+        <table tableName="%"
+            enableSelectByExample="true"
+            enableDeleteByExample="true"
+            enableCountByExample="true"
+            enableUpdateByExample="true"
+            selectByExampleQueryId="true">
+           <property name="useActualColumnNames" value="false" />
+           <property name="ignoreQualifiersAtRuntime" value="true" />
+           <property name="rootInterface" value="de.example.mybatis.mapper.filter.MyBatisScanFilter" />
+           <!--
+            <mybatis:scan/> supports filtering the mappers created by either specifying a marker interface or an annotation
+           -->
+           <property name="rootInterface" value="de.example.mybatis.mapper.filter.MyBatisScanFilter" />
+            <generatedKey column="id" sqlStatement="MySql" identity="true" type="post" />
+        </table>
     </context>
 </generatorConfiguration>
index 0fa5d05..d3ed00f 100644 (file)
     <bean id="exampleService" class="de.example.mybatis.spring.service.ExampleService">
         <property name="adMapper" ref="adMapper" />
     </bean>
+
+    <bean id="exampleCustomService" class="de.example.mybatis.spring.service.ExampleCustomService">
+        <property name="adCustomMapper" ref="adCustomMapper" />
+    </bean>
     
 </beans>