Mybatis 日志(SLF4j)

之前我们介绍了使用JDK Log、Apache Commons Logging、Log4j2打印Mybatis运行时的日志;本篇我们介绍使用SLF4j打印Mybatis运行时的日志。

如何您对Mybatis中使用JDK Log、Apache Commons Logging、Log4j2打印Mybatis运行时的日志不太了解,可以参考:

Mybatis 日志(JDK Log)icon-default.png?t=N7T8https://blog.csdn.net/m1729339749/article/details/132565362Mybatis 日志(Apache Commons Logging)icon-default.png?t=N7T8https://blog.csdn.net/m1729339749/article/details/133266537Mybatis 日志(Log4j2)icon-default.png?t=N7T8https://blog.csdn.net/m1729339749/article/details/133300984

一、添加依赖


    org.mybatis
    mybatis
    3.4.5


    mysql
    mysql-connector-java
    5.1.49


    org.slf4j
    slf4j-api
    2.0.9


    org.slf4j
    slf4j-simple
    2.0.9

二、配置Mybatis

在mybatis-config.xml文件中配置logImpl




    
        
    

    
        
            
            
                
                
                
                
            
        
    

在配置文件中,我们配置了logImpl,值配置成了SLF4J,代表的是使用SLF4J提供的日志系统

三、SLF4J日志配置

在resources目录下新建simplelogger.properties配置文件

org.slf4j.simpleLogger.logFile=System.err
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.log.cn.horse.demo.UserInfoMapper=debug
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS
org.slf4j.simpleLogger.showThreadName=true
org.slf4j.simpleLogger.showLogName=true
org.slf4j.simpleLogger.showShortLogName=false
org.slf4j.simpleLogger.levelInBrackets=true
org.slf4j.simpleLogger.warnLevelString=WARN

四、启动程序

1、准备数据

-- 如果数据库不存在则创建数据库
CREATE DATABASE IF NOT EXISTS demo DEFAULT CHARSET utf8;
-- 切换数据库
USE demo;
-- 创建用户表
CREATE TABLE IF NOT EXISTS T_USER(
  ID INT PRIMARY KEY,
  USERNAME VARCHAR(32) NOT NULL,
  AGE INT NOT NULL
);
-- 插入用户数据
INSERT INTO T_USER(ID, USERNAME, AGE)
VALUES(1, '张三', 20),(2, '李四', 22),(3, '王五', 24);

2、StatementUtils类

在cn.horse.demo下新建StatementUtils类

StatementUtils类:

package cn.horse.demo;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;
import java.util.Objects;

public abstract class StatementUtils {

    public static void find(String statement, Object parameter) {
        // 读取mybatis配置文件
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-config.xml");
        // 根据配置创建SqlSession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(inputStream);

        SqlSession sqlSession = null;
        try {
            // 创建SqlSession
            sqlSession = sqlSessionFactory.openSession();
            // 查询用户列表
            List userInfoList = sqlSession.selectList(statement, parameter);
            for (UserInfo userInfo: userInfoList) {
                System.out.println(userInfo);
            }
        } finally {
            // 关闭会话
            if(Objects.nonNull(sqlSession)) {
                sqlSession.close();
            }
        }
    }
}

3、UserInfo类

在cn.horse.demo下新建UserInfo类

UserInfo类:

package cn.horse.demo;

public class UserInfo {

    private Integer id;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append('{');
        stringBuilder.append("id: " + this.id);
        stringBuilder.append(", ");
        stringBuilder.append("name: " + this.name);
        stringBuilder.append(", ");
        stringBuilder.append("age: " + this.age);
        stringBuilder.append('}');
        return stringBuilder.toString();
    }
}

4、UserInfoMapper配置

在resources下新建demo目录,在目录下新建UserInfoMapper.xml文件





    

在mybatis-config.xml文件中引入UserInfoMapper.xml配置文件


    

5、启动程序

package cn.horse.demo;

public class Main {
    public static void main(String[] args) {
        StatementUtils.find("cn.horse.demo.UserInfoMapper.findByAge", 21);
    }
}

执行的结果如下:

你可能感兴趣的:(Mybatis,mybatis,java)