1、日志工厂
当对数据库的操作出现异常的时候方便找出错误。
(1)不使用日志工厂,查询全部学生信息:
Student{studentno='201811', sname='zhai', sex='男', birthday='1998-11-11', classno='80501', point='890', phone='1234567890', email='null', clas=null}
Student{studentno='201812', sname='zhai2', sex='男', birthday='1998-11-11', classno='80601', point='893', phone='19837372533', email='null', clas=null}
Student{studentno='201813', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201814', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201815', sname='qwerr', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201816', sname='jiayou', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201817', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null}
Student{studentno='201818', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null}
Student{studentno='2', sname='2', sex='2', birthday='null', classno='null', point='null', phone='2', email='null', clas=null}
Student{studentno='1', sname='1', sex='1', birthday='null', classno='null', point='null', phone='1', email='null', clas=null}
Student{studentno='21', sname='21', sex='21', birthday='null', classno='null', point='null', phone='21', email='null', clas=null}
Process finished with exit code 0
控制台只有查询结果,无其他信息。
(2)mybatis内置的日志工厂,具体使用哪一个由设置决定:
a、STDOUT_LOGGING
在mybatis的核心配置文件中配置:
<properties resource="jdbc.properties"></properties> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <environments default="development"> <environment id="development">
要注意settings标签的位置。
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter. 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 1685232414. Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@64729b1e] ==> Preparing: select * from student ==> Parameters: <== Columns: studentno, birthday, classno, phone, sex, sname, point, email <== Row: 201811, 1998-11-11, 80501, 1234567890, 男, zhai, 890, null <== Row: 201812, 1998-11-11, 80601, 19837372533, 男, zhai2, 893, null <== Row: 201813, 1998-11-11, 80501, 19837372534, 男, zhai3, 892, null <== Row: 201814, 1998-11-11, 80501, 19837372534, 男, zhai3, 892, null <== Row: 201815, 1998-11-11, 80501, 19837372534, 男, qwerr, 892, null <== Row: 201816, 1998-11-11, 80501, 19837372534, 男, jiayou, 892, null <== Row: 201817, null, 2, null, null, null, null, null <== Row: 201818, null, 2, null, null, null, null, null <== Row: 2, null, null, 2, 2, 2, null, null <== Row: 1, null, null, 1, 1, 1, null, null <== Row: 21, null, null, 21, 21, 21, null, null <== Total: 11 Student{studentno='201811', sname='zhai', sex='男', birthday='1998-11-11', classno='80501', point='890', phone='1234567890', email='null', clas=null} Student{studentno='201812', sname='zhai2', sex='男', birthday='1998-11-11', classno='80601', point='893', phone='19837372533', email='null', clas=null} Student{studentno='201813', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201814', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201815', sname='qwerr', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201816', sname='jiayou', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201817', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null} Student{studentno='201818', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null} Student{studentno='2', sname='2', sex='2', birthday='null', classno='null', point='null', phone='2', email='null', clas=null} Student{studentno='1', sname='1', sex='1', birthday='null', classno='null', point='null', phone='1', email='null', clas=null} Student{studentno='21', sname='21', sex='21', birthday='null', classno='null', point='null', phone='21', email='null', clas=null}
(3)LOG4J
Log4j是Apache的一个开源项目,可以控制日志信息输出的目的地是控制台、文件、GUI组件
可以控制每一条日志的输出格式
通过定义每一条日志信息的级别,能够更加细致地控制日志的生成过程
可以通过日志文件进行灵活的配置,不需要修改应用的代码
a、配置(mybatis的核心配置文件)
<properties resource="jdbc.properties"></properties> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <environments default="development">
b、导包(依赖)
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
c、创建LOG4J的配置文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在此配置文件中可以根据需求配置需要打印的其他信息
d、测试
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. 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] - Opening JDBC Connection DEBUG [main] - Created connection 19717364. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@12cdcf4] DEBUG [main] - ==> Preparing: select * from student DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 11 Student{studentno='201811', sname='zhai', sex='男', birthday='1998-11-11', classno='80501', point='890', phone='1234567890', email='null', clas=null} Student{studentno='201812', sname='zhai2', sex='男', birthday='1998-11-11', classno='80601', point='893', phone='19837372533', email='null', clas=null} Student{studentno='201813', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201814', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201815', sname='qwerr', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201816', sname='jiayou', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null} Student{studentno='201817', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null} Student{studentno='201818', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null} Student{studentno='2', sname='2', sex='2', birthday='null', classno='null', point='null', phone='2', email='null', clas=null} Student{studentno='1', sname='1', sex='1', birthday='null', classno='null', point='null', phone='1', email='null', clas=null} Student{studentno='21', sname='21', sex='21', birthday='null', classno='null', point='null', phone='21', email='null', clas=null}