MyBatis: log4j.xml, improving maps-config.xml
authorGustavo Martin Morcuende <gustavo@supergusarapo.(none)>
Tue, 27 Aug 2013 15:16:08 +0000 (17:16 +0200)
committerGustavo Martin Morcuende <gustavo@supergusarapo.(none)>
Tue, 27 Aug 2013 15:16:08 +0000 (17:16 +0200)
MyBatis/pom.xml
MyBatis/src/main/java/de/example/mybatis/TestMain.java
MyBatis/src/main/resources/log4j.xml [new file with mode: 0644]
MyBatis/src/main/resources/mybatis-sql-maps-config.xml

index a38961e..0c7e15e 100644 (file)
         <version>5.1.9</version>
     </dependency>
     <dependency>
-        <groupId>log4j</groupId>
-        <artifactId>log4j</artifactId>
-        <version>1.2.17</version>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <version>1.7.5</version>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+      <version>1.7.5</version>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <version>1.2.17</version>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.1.1</version>
+    </dependency>
+    <dependency>
+      <groupId>cglib</groupId>
+      <artifactId>cglib</artifactId>
+      <version>2.2.2</version>
     </dependency>
   </dependencies>
 </project>
index 2db41c4..9e14d0c 100644 (file)
@@ -11,9 +11,6 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import de.example.mybatis.model.Ad;
 import de.example.mybatis.repository.mapper.AdMapper;
 
-//import de.example.ibatis.demo.dao.UserDao;
-//import de.example.ibatis.demo.dao.UserDaoIbatis;
-//import de.example.ibatis.dto.UserTEO;
 
 public class TestMain {
 
@@ -79,7 +76,7 @@ public class TestMain {
         // EN EL CLASSPATH:
         final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
         .build(/**TestMain.class.getResourceAsStream("sql-maps-config.xml")**/
-                Resources.getResourceAsStream("mybatis-sql-maps-config.xml"));
+                Resources.getResourceAsStream("mybatis-sql-maps-config.xml"), "mybatisexample");
         final SqlSession session = sqlSessionFactory.openSession();
         // EN MYBATIS HAY UNA FORMA MEJOR QUE ESTA
         //             try {
@@ -88,60 +85,21 @@ public class TestMain {
         //                     session.close();
         //             }
         // ESTA FORMA ES MUCHO MEJOR :)
+
         try {
             final AdMapper mapper = session.getMapper(AdMapper.class);
             final Ad adprueba = new Ad();
+            adprueba.setAdMobileImage("lol");
             adprueba.setCompanyCategId(200L);
             adprueba.setCreatedAt(new Date());
             adprueba.setCompanyId(2L);
             adprueba.setUpdatedAt(new Date());
             mapper.insert(adprueba);
+            session.commit();
             //                 Ad ad = mapper.selectByPrimaryKey(1000L);
         } finally {
             session.close();
         }
-
-        // De esa forma consigo que el encoding dependa del valor puesto en la cabecera del xml :)
-
-        //             SqlSession session = sqlSessionFactory.openSession();
-        //             try {
-        //               Blog blog = session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
-        //             } finally {
-        //               session.close();
-        //             }
-        //
-        //             SqlSession session = sqlSessionFactory.openSession();
-        //             try {
-        //               BlogMapper mapper = session.getMapper(BlogMapper.class);
-        //               Blog blog = mapper.selectBlog(101);
-        //             } finally {
-        //               session.close();
-        //             }
-        //             SqlMapClient sqlmapClient = SqlMapClientBuilder.buildSqlMapClient (reader);
-        //
-        //             //Create a new user to persist
-        //             UserTEO user = new UserTEO();
-        //             user.setId(1);
-        //             user.setName("Demo User");
-        //             user.setPassword("password");
-        //             user.setEmail("demo-user@howtodoinjava.com");
-        //             user.setStatus(1);
-        //
-        //             //Add the user
-        //             manager.addUser(user,sqlmapClient);
-        //
-        //             //Fetch the user detail
-        //             UserTEO createdUser = manager.getUserById(4, sqlmapClient);
-        //             System.out.println(createdUser.getEmail());
-        //
-        //             //Lets delete the user
-        //             manager.deleteUserById(1, sqlmapClient);
-        //             try {
-        //                     Thread.sleep(100000);
-        //             } catch (InterruptedException e) {
-        //                     // TODO Auto-generated catch block
-        //                     e.printStackTrace();
-        //             }
     }
 
 }
diff --git a/MyBatis/src/main/resources/log4j.xml b/MyBatis/src/main/resources/log4j.xml
new file mode 100644 (file)
index 0000000..7c75361
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
+<log4j:configuration>
+ <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
+   <layout class="org.apache.log4j.PatternLayout">
+     <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L %m%n"/>
+   </layout>
+ </appender>
+ <root>
+   <priority value="debug"></priority>
+     <appender-ref ref="stdout"/>
+ </root>
+</log4j:configuration>
\ No newline at end of file
index 5ec8c90..982dc10 100644 (file)
@@ -2,13 +2,33 @@
 <!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
+
 <configuration>
-  <environments default="development">
-    <environment id="development">
+<settings>
+    <setting name="cacheEnabled" value="false"/>
+    <setting name="lazyLoadingEnabled" value="false"/>
+    <setting name="aggressiveLazyLoading" value="false"/>
+    <setting name="multipleResultSetsEnabled" value="true"/>
+    <setting name="useColumnLabel" value="false"/>
+    <setting name="useGeneratedKeys" value="false"/>
+    <setting name="autoMappingBehavior" value="PARTIAL"/>
+    <setting name="defaultExecutorType" value="SIMPLE"/>
+    <setting name="defaultStatementTimeout" value="5"/>
+    <setting name="safeRowBoundsEnabled" value="false"/>
+    <setting name="mapUnderscoreToCamelCase" value="false"/>
+    <setting name="localCacheScope" value="SESSION"/>
+    <setting name="jdbcTypeForNull" value="OTHER"/>
+    <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
+    <setting name="logPrefix" value="mybatislogger"/>
+    <setting name="logImpl" value="LOG4J"/>
+    <setting name="proxyFactory" value="CGLIB"/>
+  </settings>
+  <environments default="mybatisexample">
+    <environment id="mybatisexample">
       <transactionManager type="JDBC"/>
-      <dataSource type="POOLED">
+      <dataSource type="UNPOOLED">
         <property name="driver" value="com.mysql.jdbc.Driver"/>
-        <property name="url" value="jdbc:mysql://localhost:3306/mybatis_example"/>
+        <property name="url" value="jdbc:mysql://localhost:3306/mybatis_example?characterEncoding=UTF-8"/>
         <property name="username" value="root"/>
         <property name="password" value="root"/>
       </dataSource>