From 4797bcc6dedb1a8d9b34faa802d292f1e2b7cef8 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 29 Aug 2013 00:59:22 +0200 Subject: [PATCH] MyBatis-Spring: messing around with MyBatis/Spring --- MyBatis/MyBatis-Spring/README.txt | 8 ++ MyBatis/MyBatis-Spring/createdatabase.sh | 9 ++ MyBatis/MyBatis-Spring/pom.xml | 125 +++++++++++++++++++++ .../mybatis/spring/SpringContextLocator.java | 62 ++++++++++ .../java/de/example/mybatis/spring/TestMain.java | 20 ++++ .../mybatis/spring/service/ExampleService.java | 56 +++++++++ .../src/main/resources/config/mybatis-config.xml | 26 +++++ .../main/resources/generator/generatorConfig.xml | 70 ++++++++++++ .../MyBatis-Spring/src/main/resources/log4j.xml | 13 +++ .../src/main/resources/spring-config.xml | 104 +++++++++++++++++ 10 files changed, 493 insertions(+) create mode 100644 MyBatis/MyBatis-Spring/README.txt create mode 100755 MyBatis/MyBatis-Spring/createdatabase.sh create mode 100644 MyBatis/MyBatis-Spring/pom.xml create mode 100644 MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/SpringContextLocator.java create mode 100644 MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java create mode 100644 MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleService.java create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/config/mybatis-config.xml create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/log4j.xml create mode 100644 MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml diff --git a/MyBatis/MyBatis-Spring/README.txt b/MyBatis/MyBatis-Spring/README.txt new file mode 100644 index 0000000..3511f09 --- /dev/null +++ b/MyBatis/MyBatis-Spring/README.txt @@ -0,0 +1,8 @@ +Create database with createdatabase.sh script. + +export M2_HOME=/opt/maven/apache-maven-2.2.1 +PATH=$M2_HOME/bin:$PATH + +mvn clean install -Dmaven.test.skip=true +mvn dependency:sources +mvn dependency:resolve -Dclassifier=javadoc diff --git a/MyBatis/MyBatis-Spring/createdatabase.sh b/MyBatis/MyBatis-Spring/createdatabase.sh new file mode 100755 index 0000000..ef2bb57 --- /dev/null +++ b/MyBatis/MyBatis-Spring/createdatabase.sh @@ -0,0 +1,9 @@ +#!/bin/bash +mysql -uroot -proot -e "CREATE DATABASE mybatis_example DEFAULT CHARACTER SET utf8" + +mysql -uroot -proot -e "USE mybatis_example; CREATE TABLE ad (id SERIAL, company_id BIGINT, company_categ_id BIGINT, ad_gps BLOB, ad_mobile_image varchar(255), created_at TIMESTAMP NOT NULL, updated_at TIMESTAMP NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB, DEFAULT CHARSET=utf8, COLLATE=utf8_unicode_ci" + +# Por alguna extraña razon no pude poner el id de esta tabla como serial y no tiene ahora este id autoincrement +# Daba un error raro diciendo que no podia haber 2 claves con autoincrement en la misma tabla. Pero sí se puede... +# debo estar haciendo algo mal... :( +mysql -uroot -proot -e "USE mybatis_example; CREATE TABLE ad_description (id BIGINT(20) UNSIGNED NOT NULL, laguage_id BIGINT NOT NULL, ad_id SERIAL NOT NULL, ad_name VARCHAR(255) NOT NULL, ad_description LONGTEXT, ad_mobile_text VARCHAR(500) NOT NULL, ad_link VARCHAR(3000) NOT NULL, PRIMARY KEY (id), INDEX(ad_id), FOREIGN KEY (ad_id) REFERENCES ad (id) ON DELETE CASCADE) ENGINE=InnoDB, DEFAULT CHARSET=utf8, COLLATE=utf8_unicode_ci" diff --git a/MyBatis/MyBatis-Spring/pom.xml b/MyBatis/MyBatis-Spring/pom.xml new file mode 100644 index 0000000..21ca06c --- /dev/null +++ b/MyBatis/MyBatis-Spring/pom.xml @@ -0,0 +1,125 @@ + + 4.0.0 + de.example.mybatis + mybatis-spring-example + jar + 1.0-SNAPSHOT + mybatis-spring-example + http://gumartinm.name + + ${project.build.directory}/generated-sources/mybatis-generator/ + + + + org.mybatis + mybatis-spring + 1.2.0 + + + org.mybatis + mybatis + 3.2.2 + + + org.springframework + spring-context + 3.2.4.RELEASE + + + org.springframework + spring-jdbc + 3.2.4.RELEASE + + + com.mchange + c3p0 + 0.9.2.1 + + + mysql + mysql-connector-java + 5.1.9 + + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + slf4j-log4j12 + 1.7.5 + + + cglib + cglib + 2.2.2 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + ${project.build.sourceEncoding} + + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.2 + + + Generate MyBatis Artifacts + + generate + + + + + ${basedir}/src/main/resources/generator/generatorConfig.xml + com.mysql.jdbc.Driver + root + jdbc:mysql://localhost:3306/mybatis_example + root + ${mybatis.generator.outputdirectory} + true + true + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.8 + + + add-source + generate-sources + + add-source + + + + ${mybatis.generator.outputdirectory} + + + + + + + + diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/SpringContextLocator.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/SpringContextLocator.java new file mode 100644 index 0000000..4030e8b --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/SpringContextLocator.java @@ -0,0 +1,62 @@ +package de.example.mybatis.spring; + +import org.apache.log4j.Logger; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * Spring context locator. + * + */ +public final class SpringContextLocator { + private static final Logger logger = Logger.getLogger(SpringContextLocator.class); + + /** Spring ApplicationContext **/ + private final ApplicationContext context; + + /** Spring Context **/ + private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + final String[] factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + logger.info("Loading context files " + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + this.context = new ClassPathXmlApplicationContext(factoryFiles); + + logger.info("The context has been loaded successfully!! "); + } + + /** + * SingletonHolder Thread-safety. To use an Enum class (see Effective Java + * Second Edition) if we need serialization and thread-safety. + */ + private static class SingletonHolder { + public static final SpringContextLocator INSTANCE = new SpringContextLocator(); + } + + /** + * Return singleton instance. Thread-safety. + * + * @return Singleton instance. + */ + public static SpringContextLocator getInstance() { + return SingletonHolder.INSTANCE; + } + + /** + * Return bean from application context. + * + * @param beanId + * Bean's id. + * @return The bean instance. + */ + public Object getBean(final String beanId) { + return this.context.getBean(beanId); + } +} 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 new file mode 100644 index 0000000..19caac1 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/TestMain.java @@ -0,0 +1,20 @@ +package de.example.mybatis.spring; + +import org.apache.log4j.Logger; + +import de.example.mybatis.spring.service.ExampleService; + +public class TestMain { + private static final Logger logger = Logger.getLogger(TestMain.class); + + public static void main(final String[] args) { + + logger.info("Starting application"); + + final ExampleService exampleService = (ExampleService) SpringContextLocator + .getInstance().getBean("exampleService"); + + exampleService.getAdsByCriteria(); + } + +} diff --git a/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleService.java b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleService.java new file mode 100644 index 0000000..f397e61 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/java/de/example/mybatis/spring/service/ExampleService.java @@ -0,0 +1,56 @@ +package de.example.mybatis.spring.service; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.List; + +import org.apache.log4j.Logger; +import org.springframework.transaction.annotation.Transactional; + +import de.example.mybatis.model.Ad; +import de.example.mybatis.model.AdCriteria; +import de.example.mybatis.repository.mapper.AdMapper; + +public class ExampleService { + private static final Logger logger = Logger.getLogger(ExampleService.class); + + private AdMapper adMapper; + + public void setAdMapper(final AdMapper adMapper) { + this.adMapper = adMapper; + } + + @Transactional /**There is not inserts so this is useless, anyhow this is just an example**/ + public void getAdsByCriteria() { + logger.info("Using criteria"); + + final AdCriteria adCriteria = new AdCriteria(); + + adCriteria.or().andAdMobileImageEqualTo("mobileImage.jpg") + .andCreatedAtNotEqualTo(new Date()); + + adCriteria.or().andAdMobileImageNotEqualTo("noMobileImage.jpg") + .andAdMobileImageIsNotNull(); + + // where (ad_mobile_image = "mobileImage.jpg" and created_at <> Now()) + // or (ad_mobile_image <> "noMobileImage.jpg" and ad_mobile_image is not null) + + final List adLists = this.adMapper.selectByExampleWithBLOBs(adCriteria); + for (final Ad ad : adLists) { + 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"); + } + } +} \ No newline at end of file diff --git a/MyBatis/MyBatis-Spring/src/main/resources/config/mybatis-config.xml b/MyBatis/MyBatis-Spring/src/main/resources/config/mybatis-config.xml new file mode 100644 index 0000000..43999df --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/config/mybatis-config.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ 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 new file mode 100644 index 0000000..652403a --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/generator/generatorConfig.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+
+
diff --git a/MyBatis/MyBatis-Spring/src/main/resources/log4j.xml b/MyBatis/MyBatis-Spring/src/main/resources/log4j.xml new file mode 100644 index 0000000..7c75361 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/log4j.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ 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 new file mode 100644 index 0000000..91a76d5 --- /dev/null +++ b/MyBatis/MyBatis-Spring/src/main/resources/spring-config.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.1.4