之前我们介绍了使用JDK Log、Apache Commons Logging打印Mybatis运行时的日志;本篇我们介绍使用Log4j2打印Mybatis运行时的日志。
如何您对Mybatis中使用JDK Log、Apache Commons Logging打印Mybatis运行时的日志不太了解,可以参考:
Mybatis 日志(JDK Log)https://blog.csdn.net/m1729339749/article/details/132565362Mybatis 日志(Apache Commons Logging)https://blog.csdn.net/m1729339749/article/details/133266537
一、添加依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
二、配置Mybatis
在mybatis-config.xml文件中配置logImpl
<?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>
<settings>
<setting name="logImpl" value="LOG4J2"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo?useUnicode=true&useSSL=false&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="horse"/>
</dataSource>
</environment>
</environments>
</configuration>
在配置文件中,我们配置了logImpl,值配置成了LOG4J2,代表的是使用Log4j2提供的日志系统
三、Log4j2日志配置
1、log4j2.component.properties
在resources目录下新建log4j2.component.properties配置文件
log4j.configurationFile=log4j2.properties
log4j.configurationFile用于指定log4j2的配置文件
2、log4j2.properties
在resources目录下新建log4j2.properties配置文件
status = ERROR
logger.action.name = cn.horse.demo.UserInfoMapper
logger.action.level = debug
log4j2.properties配置文件中配置了日志的级别
四、启动程序
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<UserInfo> 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文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.horse.demo.UserInfoMapper">
<select id="findByAge" resultType="cn.horse.demo.UserInfo">
SELECT
ID,
USERNAME name,
AGE
FROM T_USER
WHERE AGE > #{age}
</select>
</mapper>
在mybatis-config.xml文件中引入UserInfoMapper.xml配置文件
<mappers>
<mapper resource="demo/UserInfoMapper.xml" />
</mappers>
5、启动程序
package cn.horse.demo;
public class Main {
public static void main(String[] args) {
System.setProperty("java.util.logging.config.class", "cn.horse.demo.JdkLogConfig");
StatementUtils.find("cn.horse.demo.UserInfoMapper.findByAge", 21);
}
}
执行的结果如下: