Mybatis的日志以及動(dòng)態(tài)sql

Mybatis的日志以及動(dòng)態(tài)sql

項(xiàng)目中添加junit,mysql,mybatis以及l(fā)og4j的依賴

<?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>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38

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">

<!--
    該配置文件中包含一個(gè)configuration節(jié)點(diǎn)
        里面有配置信息 分別是環(huán)境和映射
         其中環(huán)境里有datasource,里面有我們熟悉的連接數(shù)據(jù)庫的四個(gè)字符串
-->
<configuration>

    <!--
        引入db的配置文件信息,后面用到的四個(gè)連接字符串就可以直接使用 ${}的方式來動(dòng)態(tài)引入
    -->
    <properties resource="db.properties">
    </properties>

    <!--
        給當(dāng)前mybatis項(xiàng)目添加日志功能,該STDOUT_LOGGING值的好處是不用添加第三方j(luò)ar包就可以有日志的輸出
    -->
    <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>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hello
user=root
pass=123456
    1
    2
    3
    4

log4j.properties

# 全局日志配置
#   日志有四個(gè)級(jí)別:degbug,warn,info,debug
log4j.rootLogger=debug, stdout, F
# MyBatis 日志配置
log4j.logger.com.qfedu=TRACE
# 控制臺(tái)輸出
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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

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;
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

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();
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

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">

<!--
    第二種實(shí)現(xiàn)mybatis的方式為:接口+xml方式
    這種方式有特別的要求
        1.  namespace必須是接口的全路徑
        2.  每個(gè)節(jié)點(diǎn)的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>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50

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對(duì)象
     * @return
     */
    public static SqlSession getSession(){

        mSession = mFactory.openSession(true);

        return mSession;
    }

    /**
     * 關(guān)閉SqlSession對(duì)象
     * @param session 要關(guān)閉的SqlSession對(duì)象
     */
    public static void closeSession(SqlSession session){
        if(session != null){
            session.close();
            session = null;
        }
    }
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45

控制臺(tái)打印的日志結(jié)果

/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  ?U?V
 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  ?U?V
 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容