日志
在Settings标签中可以设置logImpl,用于指定 MyBatis 所用日志的具体实现
键为logImpl,值为:
SLF4J
LOG4J
LOG4J2
JDK_LOGGING
COMMONS_LOGGING
STDOUT_LOGGING(标准格式)
NO_LOGGING
1.当值为STDOUT_LOGGING时
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
结果
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 633240419.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25be7b63]
==> Preparing: select * from user where name like "%" ? "%"
==> Parameters: 李(String)
<== Columns: id, name, pwd
<== Row: 3, 李四, 1231233
<== Row: 4, 李三, 123123
<== Total: 2
User{id=3, name='李四', password='1231233'}
User{id=4, name='李三', password='123123'}
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@25be7b63]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@25be7b63]
Returned connection 633240419 to pool.
2.log4j
2.1什么是log4j?
- 通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件
- 可以控制每一条日志的输出格式
- 通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程
- 可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
2.2导包
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.3 编写log4j.propertise配置文件(注意名字一定要为log4j,这是要求,要不然会找不到配置文件)
log4j.rootCategory=DEBUG,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold =DEBUG
#PatternLayout(可以灵活的自定义布局)
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#DailyRollingFileAppender (每天产生一个日志文件)
#FileAppender(文件)
log4j.appender.file=org.apache.log4j.RollingFileAppender
#定义log日记生成的路径
log4j.appender.file.File=./log/luo.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#指定com.Google包下的所有类的等级为DEBUG。
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
2.4在对应的类中使用该方法
2.4.1 定义一个静态的全局变量(以便后面方法,好用来测试)
static Logger logger = Logger.getLogger(userMapperTest.class);
2.4.2 测试
public void getUserByID2(){
logger.debug("欢迎来到Debug的世界");
logger.info("欢迎来到Info的世界");
logger.error("欢迎来到error的世界");
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
userMapper mapper = sqlSession.getMapper(userMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("name","李");
List<User> userByID2 = mapper.getUserByID2(map);
for (User user : userByID2) {
System.out.println(user);
}
sqlSession.close();
}
2.4.3 运行结果
[DEBUG][22-01-19][com.Google.Dao.userMapperTest]欢迎来到Debug的世界
[INFO][22-01-19][com.Google.Dao.userMapperTest]欢迎来到Info的世界
[ERROR][22-01-19][com.Google.Dao.userMapperTest]欢迎来到error的世界
[DEBUG][22-01-19][org.apache.ibatis.logging.LogFactory]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[DEBUG][22-01-19][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-01-19][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-01-19][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-01-19][org.apache.ibatis.datasource.pooled.PooledDataSource]PooledDataSource forcefully closed/removed all connections.
[DEBUG][22-01-19][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Opening JDBC Connection
[DEBUG][22-01-19][org.apache.ibatis.datasource.pooled.PooledDataSource]Created connection 1693226694.
[DEBUG][22-01-19][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@64ec96c6]
[DEBUG][22-01-19][com.Google.Dao.userMapper.getUserByID2]==> Preparing: select * from user where name like "%" ? "%"
[DEBUG][22-01-19][com.Google.Dao.userMapper.getUserByID2]==> Parameters: 李(String)
[DEBUG][22-01-19][com.Google.Dao.userMapper.getUserByID2]<== Total: 2
[DEBUG][22-01-19][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@64ec96c6]
[DEBUG][22-01-19][org.apache.ibatis.transaction.jdbc.JdbcTransaction]Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@64ec96c6]
[DEBUG][22-01-19][org.apache.ibatis.datasource.pooled.PooledDataSource]Returned connection 1693226694 to pool.