Mybatis的日志以及动态sql

Mybatis的日志以及动态sql

  1. 项目中添加junit,mysql,mybatis以及log4j的依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.qfedu</groupId>
        <artifactId>Days25MyBatis04</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.44</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.4</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.6</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    </project>
    
  2. Mybatis.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <!--
        该配置文件中包含一个configuration节点
            里面有配置信息 分别是环境和映射
             其中环境里有datasource,里面有我们熟悉的连接数据库的四个字符串
    -->
    <configuration>
    
        <!--
            引入db的配置文件信息,后面用到的四个连接字符串就可以直接使用 ${}的方式来动态引入
        -->
        <properties resource="db.properties">
        </properties>
    
        <!--
            给当前mybatis项目添加日志功能,该STDOUT_LOGGING值的好处是不用添加第三方jar包就可以有日志的输出
        -->
        <settings>
            <setting name="logImpl" value="LOG4J"/>
        </settings>
    
        <typeAliases>
            <!--<typeAlias type="com.qfedu.pojo.Order" alias="abc" />-->
            <package name="com.qfedu.pojo" />
        </typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${user}"/>
                    <property name="password" value="${pass}"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <!--<mapper resource="com/qfedu/pojo/IUserDao.xml"/>-->
            <package name="com.qfedu.pojo" />
        </mappers>
    </configuration>
    
  3. db.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/hello
    user=root
    pass=123456
    
  4. log4j.properties

    # 全局日志配置
    #   日志有四个级别:degbug,warn,info,debug
    log4j.rootLogger=debug, stdout, F
    # MyBatis 日志配置
    log4j.logger.com.qfedu=TRACE
    # 控制台输出
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%6p [%t] - %m%n
    
    log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.F.File =myproj.log
    log4j.appender.F.Append = true
    log4j.appender.F.Threshold = DEBUG
    log4j.appender.F.layout=org.apache.log4j.PatternLayout
    log4j.appender.F.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss}-[%p %F\:%L]  %m%n
    
  5. User.java

    package com.qfedu.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
    
        private int uid;
        private String username;
        private String password;
        private int age;
        private String addr;
    }
    
  6. IUserDao.java

    package com.qfedu.pojo;
    
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    public interface IUserDao {
    
        @Select("select * from user")
        List<User> getAll();
    }
    
  7. IUserDao.xml

    <?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">
    
    <!--
        第二种实现mybatis的方式为:接口+xml方式
        这种方式有特别的要求
            1.  namespace必须是接口的全路径
            2.  每个节点的id必须是接口中的方法名
            3.  该接口中的方法不允许重载,否则namespace+id将不唯一
            4.  注意该接口中的增删改的方法的返回值,最好使用int
    -->
    <mapper namespace="com.qfedu.pojo.IUserDao">
    
        <sql id="all">
            select * from user
        </sql>
    
        <select id="selectAll" resultType="user">
            <include refid="all"/>
        </select>
    
        <select id="selectUserByUid" resultType="user">
            <include refid="all"/>
            where uid = #{uid}
        </select>
    
        <select id="selectIf" resultType="user">
            <include refid="all"/>
            <where>
                <if test="username != null">
                    username = #{username}
                </if>
                <if test="password != null">
                    and password = #{password}
                </if>
            </where>
        </select>
    
        <select id="selectIn" resultType="user">
            <include refid="all" />
            <where>
                uid in
                <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </where>
        </select>
    </mapper>
    
  8. SessionUtils.java

    package com.qfedu.util;
    
    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 java.io.IOException;
    
    public class SessionUtils {
    
        private static SqlSession mSession = null;
        private static SqlSessionFactory mFactory = null;
    
        static {
            try {
                mFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 获取SqlSession对象
         * @return
         */
        public static SqlSession getSession(){
    
            mSession = mFactory.openSession(true);
    
            return mSession;
        }
    
        /**
         * 关闭SqlSession对象
         * @param session 要关闭的SqlSession对象
         */
        public static void closeSession(SqlSession session){
            if(session != null){
                session.close();
                session = null;
            }
        }
    }
    
  9. 控制台打印的日志结果

    /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=53701:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/lib/tools.jar:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/test-classes:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes:/Users/james/Documents/doc/repository/junit/junit/4.12/junit-4.12.jar:/Users/james/Documents/doc/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/james/Documents/doc/repository/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar:/Users/james/Documents/doc/repository/org/mybatis/mybatis/3.4.4/mybatis-3.4.4.jar:/Users/james/Documents/doc/repository/org/projectlombok/lombok/1.18.6/lombok-1.18.6.jar:/Users/james/Documents/doc/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.qfedu.test.TestLog,testLog
     DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
     DEBUG [main] - Class not found: org.jboss.vfs.VFS
     DEBUG [main] - JBoss 6 VFS API is not available in this environment.
     DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
     DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
     DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo
     DEBUG [main] - Reader entry: IUserDao.class
     DEBUG [main] - Reader entry: IUserDao.xml
     DEBUG [main] - Reader entry: User.class
     DEBUG [main] - Listing file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.class
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.class
     DEBUG [main] - Reader entry: ����4
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.xml
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.xml
     DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/User.class
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/User.class
     DEBUG [main] - Reader entry: ����4n	Q	R	S	T	UV
     DEBUG [main] - Checking to see if class com.qfedu.pojo.IUserDao matches criteria [is assignable to Object]
     DEBUG [main] - Checking to see if class com.qfedu.pojo.User matches criteria [is assignable to Object]
     DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
     DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
     DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
     DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
     DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo
     DEBUG [main] - Reader entry: IUserDao.class
     DEBUG [main] - Reader entry: IUserDao.xml
     DEBUG [main] - Reader entry: User.class
     DEBUG [main] - Listing file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.class
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.class
     DEBUG [main] - Reader entry: ����4
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.xml
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/IUserDao.xml
     DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
     DEBUG [main] - Find JAR URL: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/User.class
     DEBUG [main] - Not a JAR: file:/Users/james/Documents/NZ1903/lesson/Days25MyBatis04/target/classes/com/qfedu/pojo/User.class
     DEBUG [main] - Reader entry: ����4n	Q	R	S	T	UV
     DEBUG [main] - Checking to see if class com.qfedu.pojo.IUserDao matches criteria [is assignable to Object]
     DEBUG [main] - Checking to see if class com.qfedu.pojo.User matches criteria [is assignable to Object]
     DEBUG [main] - Opening JDBC Connection
    Fri Mar 13 16:43:20 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
     DEBUG [main] - Created connection 452805835.
     DEBUG [main] - ==>  Preparing: select * from user 
     DEBUG [main] - ==> Parameters: 
     TRACE [main] - <==    Columns: uid, username, password, age, addr
     TRACE [main] - <==        Row: 1, sunwukong, 111111, 500, huaguoshan
     TRACE [main] - <==        Row: 2, zhubajie, 222222, 2000, gaolaozhuang
     TRACE [main] - <==        Row: 3, shaheshang, 333333, 500, liushahe
     TRACE [main] - <==        Row: 4, tangtang, 444444, 20, datangwangchao
     TRACE [main] - <==        Row: 5, baikongma, 555555, 100, longgong
     TRACE [main] - <==        Row: 6, lisixin6, lisixin, 20, beijing
     TRACE [main] - <==        Row: 7, baigujing, 888888, 800, baigudong
     TRACE [main] - <==        Row: 8, username8, 888888, 20, test
     TRACE [main] - <==        Row: 16, lisixin, lisixin, 20, beijing
     TRACE [main] - <==        Row: 17, chenggang, 888888, 20, shenzhen
     TRACE [main] - <==        Row: 18, lishushu, 888888, 20, beijing
     TRACE [main] - <==        Row: 19, liuchang, 888888, 20, guangzhou
     TRACE [main] - <==        Row: 20, xiaoyao, 777777, 16, wuhan
     DEBUG [main] - <==      Total: 13
    User(uid=1, username=sunwukong, password=111111, age=500, addr=huaguoshan)
    User(uid=2, username=zhubajie, password=222222, age=2000, addr=gaolaozhuang)
    User(uid=3, username=shaheshang, password=333333, age=500, addr=liushahe)
    User(uid=4, username=tangtang, password=444444, age=20, addr=datangwangchao)
    User(uid=5, username=baikongma, password=555555, age=100, addr=longgong)
    User(uid=6, username=lisixin6, password=lisixin, age=20, addr=beijing)
    User(uid=7, username=baigujing, password=888888, age=800, addr=baigudong)
    User(uid=8, username=username8, password=888888, age=20, addr=test)
    User(uid=16, username=lisixin, password=lisixin, age=20, addr=beijing)
    User(uid=17, username=chenggang, password=888888, age=20, addr=shenzhen)
    User(uid=18, username=lishushu, password=888888, age=20, addr=beijing)
    User(uid=19, username=liuchang, password=888888, age=20, addr=guangzhou)
    User(uid=20, username=xiaoyao, password=777777, age=16, addr=wuhan)
     DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1afd44cb]
     DEBUG [main] - Returned connection 452805835 to pool.
    
    Process finished with exit code 0
    
发布了19 篇原创文章 · 获赞 37 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zpz2001/article/details/104844487