From: Gustavo Martin Morcuende Date: Wed, 28 Aug 2013 16:12:14 +0000 (+0200) Subject: MyBatis: new project MyBatis-Spring X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=b269d16da72609ad37746c4ac822a84bfdecb787;p=JavaForFun MyBatis: new project MyBatis-Spring --- diff --git a/MyBatis/MyBatis/README.txt b/MyBatis/MyBatis/README.txt new file mode 100644 index 0000000..3511f09 --- /dev/null +++ b/MyBatis/MyBatis/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/createdatabase.sh b/MyBatis/MyBatis/createdatabase.sh new file mode 100755 index 0000000..ef2bb57 --- /dev/null +++ b/MyBatis/MyBatis/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/pom.xml b/MyBatis/MyBatis/pom.xml new file mode 100644 index 0000000..f34609b --- /dev/null +++ b/MyBatis/MyBatis/pom.xml @@ -0,0 +1,121 @@ + + 4.0.0 + de.example.mybatis + mybatisexample + jar + 1.0-SNAPSHOT + mybatisexample + http://gumartinm.name + + ${project.build.directory}/generated-sources/mybatis-generator/ + + + + + 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} + + + + + + + + + + junit + junit + 3.8.1 + test + + + org.mybatis + mybatis + 3.2.2 + + + mysql + mysql-connector-java + 5.1.9 + + + org.slf4j + slf4j-api + 1.7.5 + + + org.slf4j + slf4j-log4j12 + 1.7.5 + + + log4j + log4j + 1.2.17 + + + commons-logging + commons-logging + 1.1.1 + + + cglib + cglib + 2.2.2 + + + diff --git a/MyBatis/MyBatis/src/main/java/de/example/mybatis/TestMain.java b/MyBatis/MyBatis/src/main/java/de/example/mybatis/TestMain.java new file mode 100644 index 0000000..60e9bb2 --- /dev/null +++ b/MyBatis/MyBatis/src/main/java/de/example/mybatis/TestMain.java @@ -0,0 +1,126 @@ +package de.example.mybatis; + +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.log4j.Logger; + +import de.example.mybatis.model.Ad; +import de.example.mybatis.model.AdCriteria; +import de.example.mybatis.repository.mapper.AdMapper; + + +public class TestMain { + private static final Logger logger = Logger.getLogger(TestMain.class); + + public static void main(final String[] args) throws IOException { + + // From org.xml.sax.InputSource Javadoc: + // The SAX parser will use the InputSource object to determine how to + // read XML input. If there is a character stream available, the parser + // will read that stream directly, disregarding any text encoding + // declaration found in that stream. If there is no character stream, + // but there is a byte stream, the parser will use that byte stream, + // using the encoding specified in the InputSource or else (if no + // encoding is specified) autodetecting the character encoding using an + // algorithm such as the one in the XML specification. If neither a + // character stream nor a byte stream is available, the parser will + // attempt to open a URI connection to the resource identified by the + // system identifier. + + // Then if we use an InputStream (it is not a character stream) and + // we do not specify the encoding, the encoding should be autodetected + // reading the XML header. :) That is what I want. :) + final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() + .build(/**TestMain.class.getResourceAsStream("sql-maps-config.xml")**/ + Resources.getResourceAsStream("mybatis-sql-maps-config.xml"), "mybatisexample"); + + SqlSession session = sqlSessionFactory.openSession(); + + try { + final AdMapper adMapper = session.getMapper(AdMapper.class); + final Ad adTest = new Ad(); + adTest.setAdMobileImage("mobileImage.jpg"); + adTest.setCompanyCategId(200L); + adTest.setCreatedAt(new Date()); + adTest.setCompanyId(2L); + adTest.setUpdatedAt(new Date()); + adMapper.insert(adTest); + session.commit(); + + final List adLists = adMapper.selectByExample(null); + for (final Ad ad : adLists) { + logger.info("Ad id: " + ad.getId()); + if (ad.getAdGps() != null) { + logger.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8")); + } + 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"); + } + } finally { + session.close(); + } + + session = sqlSessionFactory.openSession(); + + try { + logger.info("Last insert"); + final AdMapper adMapper = session.getMapper(AdMapper.class); + final Ad adTest = new Ad(); + adTest.setAdMobileImage("mobileImage.jpg"); + adTest.setCompanyCategId(200L); + adTest.setCreatedAt(new Date()); + adTest.setCompanyId(2L); + adTest.setUpdatedAt(new Date()); + adMapper.insert(adTest); + session.commit(); + + } finally { + session.close(); + } + + session = sqlSessionFactory.openSession(); + + try { + 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 AdMapper adMapper = session.getMapper(AdMapper.class); + final List adLists = adMapper.selectByExampleWithBLOBs(adCriteria); + for (final Ad ad : adLists) { + logger.info("Ad id: " + ad.getId()); + if (ad.getAdGps() != null) { + logger.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8")); + } + 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"); + } + } finally { + session.close(); + } + } + +} diff --git a/MyBatis/MyBatis/src/main/resources/generator/generatorConfig.xml b/MyBatis/MyBatis/src/main/resources/generator/generatorConfig.xml new file mode 100644 index 0000000..652403a --- /dev/null +++ b/MyBatis/MyBatis/src/main/resources/generator/generatorConfig.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+
+
diff --git a/MyBatis/MyBatis/src/main/resources/log4j.xml b/MyBatis/MyBatis/src/main/resources/log4j.xml new file mode 100644 index 0000000..7c75361 --- /dev/null +++ b/MyBatis/MyBatis/src/main/resources/log4j.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/MyBatis/MyBatis/src/main/resources/mybatis-sql-maps-config.xml b/MyBatis/MyBatis/src/main/resources/mybatis-sql-maps-config.xml new file mode 100644 index 0000000..ff80a44 --- /dev/null +++ b/MyBatis/MyBatis/src/main/resources/mybatis-sql-maps-config.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MyBatis/README.txt b/MyBatis/README.txt deleted file mode 100644 index 3511f09..0000000 --- a/MyBatis/README.txt +++ /dev/null @@ -1,8 +0,0 @@ -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/createdatabase.sh b/MyBatis/createdatabase.sh deleted file mode 100755 index ef2bb57..0000000 --- a/MyBatis/createdatabase.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/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/pom.xml b/MyBatis/pom.xml deleted file mode 100644 index f34609b..0000000 --- a/MyBatis/pom.xml +++ /dev/null @@ -1,121 +0,0 @@ - - 4.0.0 - de.example.mybatis - mybatisexample - jar - 1.0-SNAPSHOT - mybatisexample - http://gumartinm.name - - ${project.build.directory}/generated-sources/mybatis-generator/ - - - - - 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} - - - - - - - - - - junit - junit - 3.8.1 - test - - - org.mybatis - mybatis - 3.2.2 - - - mysql - mysql-connector-java - 5.1.9 - - - org.slf4j - slf4j-api - 1.7.5 - - - org.slf4j - slf4j-log4j12 - 1.7.5 - - - log4j - log4j - 1.2.17 - - - commons-logging - commons-logging - 1.1.1 - - - cglib - cglib - 2.2.2 - - - diff --git a/MyBatis/src/main/java/de/example/mybatis/TestMain.java b/MyBatis/src/main/java/de/example/mybatis/TestMain.java deleted file mode 100644 index 60e9bb2..0000000 --- a/MyBatis/src/main/java/de/example/mybatis/TestMain.java +++ /dev/null @@ -1,126 +0,0 @@ -package de.example.mybatis; - -import java.io.IOException; -import java.util.Date; -import java.util.List; - -import org.apache.ibatis.io.Resources; -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.ibatis.session.SqlSessionFactoryBuilder; -import org.apache.log4j.Logger; - -import de.example.mybatis.model.Ad; -import de.example.mybatis.model.AdCriteria; -import de.example.mybatis.repository.mapper.AdMapper; - - -public class TestMain { - private static final Logger logger = Logger.getLogger(TestMain.class); - - public static void main(final String[] args) throws IOException { - - // From org.xml.sax.InputSource Javadoc: - // The SAX parser will use the InputSource object to determine how to - // read XML input. If there is a character stream available, the parser - // will read that stream directly, disregarding any text encoding - // declaration found in that stream. If there is no character stream, - // but there is a byte stream, the parser will use that byte stream, - // using the encoding specified in the InputSource or else (if no - // encoding is specified) autodetecting the character encoding using an - // algorithm such as the one in the XML specification. If neither a - // character stream nor a byte stream is available, the parser will - // attempt to open a URI connection to the resource identified by the - // system identifier. - - // Then if we use an InputStream (it is not a character stream) and - // we do not specify the encoding, the encoding should be autodetected - // reading the XML header. :) That is what I want. :) - final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() - .build(/**TestMain.class.getResourceAsStream("sql-maps-config.xml")**/ - Resources.getResourceAsStream("mybatis-sql-maps-config.xml"), "mybatisexample"); - - SqlSession session = sqlSessionFactory.openSession(); - - try { - final AdMapper adMapper = session.getMapper(AdMapper.class); - final Ad adTest = new Ad(); - adTest.setAdMobileImage("mobileImage.jpg"); - adTest.setCompanyCategId(200L); - adTest.setCreatedAt(new Date()); - adTest.setCompanyId(2L); - adTest.setUpdatedAt(new Date()); - adMapper.insert(adTest); - session.commit(); - - final List adLists = adMapper.selectByExample(null); - for (final Ad ad : adLists) { - logger.info("Ad id: " + ad.getId()); - if (ad.getAdGps() != null) { - logger.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8")); - } - 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"); - } - } finally { - session.close(); - } - - session = sqlSessionFactory.openSession(); - - try { - logger.info("Last insert"); - final AdMapper adMapper = session.getMapper(AdMapper.class); - final Ad adTest = new Ad(); - adTest.setAdMobileImage("mobileImage.jpg"); - adTest.setCompanyCategId(200L); - adTest.setCreatedAt(new Date()); - adTest.setCompanyId(2L); - adTest.setUpdatedAt(new Date()); - adMapper.insert(adTest); - session.commit(); - - } finally { - session.close(); - } - - session = sqlSessionFactory.openSession(); - - try { - 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 AdMapper adMapper = session.getMapper(AdMapper.class); - final List adLists = adMapper.selectByExampleWithBLOBs(adCriteria); - for (final Ad ad : adLists) { - logger.info("Ad id: " + ad.getId()); - if (ad.getAdGps() != null) { - logger.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8")); - } - 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"); - } - } finally { - session.close(); - } - } - -} diff --git a/MyBatis/src/main/resources/generator/generatorConfig.xml b/MyBatis/src/main/resources/generator/generatorConfig.xml deleted file mode 100644 index 652403a..0000000 --- a/MyBatis/src/main/resources/generator/generatorConfig.xml +++ /dev/null @@ -1,70 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -
-
-
diff --git a/MyBatis/src/main/resources/log4j.xml b/MyBatis/src/main/resources/log4j.xml deleted file mode 100644 index 7c75361..0000000 --- a/MyBatis/src/main/resources/log4j.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/MyBatis/src/main/resources/mybatis-sql-maps-config.xml b/MyBatis/src/main/resources/mybatis-sql-maps-config.xml deleted file mode 100644 index ff80a44..0000000 --- a/MyBatis/src/main/resources/mybatis-sql-maps-config.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file