来源:
http://mybatis.github.io/mybatis-3/logging.html
=====================================================
Logging
MyBatis provides logging information through the use of an internal log factory. The internal log factory will delegate logging information to one of the following log implementations:
- SLF4J
- Apache Commons Logging
- Log4j 2
- Log4j
- JDK logging
The logging solution chosen is based on runtime introspection by the internal MyBatis log factory. The MyBatis log factory will use the first logging implementation it finds (implementations are searched in the above order). If MyBatis finds none of the above implementations, then logging will be disabled.
Many environments ship Commons Logging as a part of the application server classpath (good examples include Tomcat and WebSphere). It is important to know that in such environments, MyBatis will use Commons Logging as the logging implementation. In an environment like WebSphere this will mean that your Log4J configuration will be ignored because WebSphere supplies its own proprietary implementation of Commons Logging. This can be very frustrating because it will appear that MyBatis is ignoring your Log4J configuration (in fact, MyBatis is ignoring your Log4J configuration because MyBatis will use Commons Logging in such environments). If your application is running in an environment where Commons Logging is included in the classpath but you would rather use one of the other logging implementations you can select a different logging implementation by adding a setting in mybatis-config.xml file as follows:
<configuration> <settings> ... <setting name="logImpl" value="LOG4J"/> ... </settings> </configuration>
Valid values are SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING or a full qualified class name that implements org.apache.ibatis.logging.Log and gets an string as a constructor parameter.
You can also select the implementation by calling one of the following methods:
org.apache.ibatis.logging.LogFactory.useSlf4jLogging(); org.apache.ibatis.logging.LogFactory.useLog4JLogging(); org.apache.ibatis.logging.LogFactory.useLog4J2Logging(); org.apache.ibatis.logging.LogFactory.useJdkLogging(); org.apache.ibatis.logging.LogFactory.useCommonsLogging(); org.apache.ibatis.logging.LogFactory.useStdOutLogging();
If you choose to call one of these methods, you should do so before calling any other MyBatis method. Also, these methods will only switch to the requested log implementation if that implementation is available on the runtime classpath. For example, if you try to select Log4J logging and Log4J is not available at runtime, then MyBatis will ignore the request to use Log4J and will use it's normal algorithm for discovering logging implementations.
The specifics of SLF4J, Apache Commons Logging, Apache Log4J and the JDK Logging API are beyond the scope of this document. However the example configuration below should get you started. If you would like to know more about these frameworks, you can get more information from the following locations:
Logging Configuration
To see MyBatis logging statements you may enable logging on a package, a mapper fully qualified class name, a namespace o a fully qualified statement name.
Again, how you do this is dependent on the logging implementation in use. We'll show how to do it with Log4J. Configuring the logging services is simply a matter of including one or more extra configuration files (e.g. log4j.properties) and sometimes a new JAR file (e.g. log4j.jar). The following example configuration will configure full logging services using Log4J as a provider. There are 2 steps.
Step 1: Add the Log4J JAR file
Because we are using Log4J, we will need to ensure its JAR file is available to our application. To use Log4J, you need to add the JAR file to your application classpath. You can download Log4J from the URL above.
For web or enterprise applications you can add the log4j.jar to your WEB-INF/lib directory, or for a standalone application you can simply add it to the JVM -classpath startup parameter.
Step 2: Configure Log4J
Configuring Log4J is simple. Suppose you want to enable the log for this mapper:
package org.mybatis.example; public interface BlogMapper { @Select("SELECT * FROM blog WHERE id = #{id}") Blog selectBlog(int id); }
Create a file called log4j.properties as shown below and place it in your classpath:
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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
The above file will cause log4J to report detailed logging for org.mybatis.example.BlogMapper and just errors for the rest of the classes of your application.
If you want to tune the logging at a finer level you can turn logging on for specific statements instead of the whole mapper file. The following line will enable logging just for the selectBlog statement:
log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE
By the contrary you may want want to enable logging for a group of mappers. In that case you should add as a logger the root package where your mappers reside:
log4j.logger.org.mybatis.example=TRACE
There are queries that can return huge result sets. In that cases you may want to see the SQL statement but not the results. For that purpose SQL statements are logged at the DEBUG level (FINE in JDK logging) and results at the TRACE level (FINER in JDK logging), so in case you want to see the statement but not the result, set the level to DEBUG.
log4j.logger.org.mybatis.example=DEBUG
But what about if you are not using mapper interfaces but mapper XML files like this one?
<?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"> <mapper namespace="org.mybatis.example.BlogMapper"> <select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper>
In that case you can enable logging for the whole XML file by adding a logger for the namespace as shown below:
log4j.logger.org.mybatis.example.BlogMapper=TRACE
Or for an specific statement:
log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE
Yes, as you may have noticed, there is no difference in configuring logging for mapper interfaces or for XML mapper files.
NOTE If you are using SLF4J or Log4j 2 MyBatis will call it using the marker MYBATIS.
The remaining configuration in the log4j.properties file is used to configure the appenders, which is beyond the scope of this document. However, you can find more information at the Log4J website (URL above). Or, you could simply experiment with it to see what effects the different configuration options have.
相关推荐
本配置文件"springboot-mybatis"主要涵盖了在2019年8月19日时,如何将这两者结合使用的基本步骤和注意事项。 1. **SpringBoot整合Mybatis** - 添加依赖:首先,要在`pom.xml`中引入SpringBoot的starter-web和...
这个压缩包“mybatis3依赖jar包”显然是为了支持MyBatis 3.x版本的运行而准备的,其中可能包含了MyBatis框架本身以及其运行所需的依赖库。 首先,MyBatis的核心组件包括MyBatis-SQLMapConfig.xml配置文件,这是整个...
MyBatis起源于iBatis,iBatis是Clinton Begin在2002年发起的开源项目,于2010年6月16日从Apache的网站退役,并被Google Code托管,改名为MyBatis。MyBatis是一个支持普通SQL查询、存储过程和高级映射的优秀的持久层...
1、基于yml 配置方式 ,实现springBoot+sharding-jdbc+mybatis-plus 实现分库分表,读写分离,以及全局表,子表的配置。 2、实现mybatis-plus 整合到springboot 详细使用请看 测试用例
java Springboot开发必备环境 : ...统一参数校验,自定义异常提醒,统一日志,统一响应返回,统一异常处理 。 推荐2: mybatis-plus 采用最新的生成代码工具 推荐3: 将多个基础功能整理后,并用单元测试验证。
3. 在MyBatis的配置文件中注册自定义的TypeHandler。 结论 ---- 本文介绍了如何使用MyBatis的自定义TypeHandler转换类型,解决了日期类型转换的问题。使用自定义TypeHandler可以提高开发效率和代码的可读性,满足...
这个压缩包包含的是Mybatis的3.4.5版本的开发jar包,发布日期为2017年8月20日,是当时Gitup上的最新版本。在本文中,我们将深入探讨Mybatis的核心特性、工作原理以及如何与JDBC进行比较。 Mybatis的主要目标是消除...
MyBatis的版本更新至3.1.1,最新更新日期为2012年4月19日。它提供了一套完整的用户文档和项目信息,帮助开发者更好地理解和使用该框架。MyBatis的配置文件主要使用XML格式,但也可使用注解。其中,Mapper XML文件...
2010年6月16日,该项目被迁移到Google Code上,并更名为MyBatis。随着Google Code的关闭,MyBatis现在托管于GitHub。 #### 2. 持久层框架对比 - **Hibernate**:一个全自动的ORM框架,它可以自动生成SQL语句并自动...
1. 配置MyBatis的全局配置文件(mybatis-config.xml),设置数据源、事务管理器等相关信息。 2. 创建Mapper接口,并在XML文件中定义对应的SQL语句和结果映射。 3. 在Spring框架中,可以使用MyBatis-Spring库集成...
`mybatisTest`文件夹可能包含了使用MyBatis进行测试的代码,这些代码通常会展示如何集成MyBatis到项目中,使用Maven进行依赖管理,以及log4j2日志框架的日志配置。在实践中,你会看到如何创建Mapper接口,编写对应的...
1. **Mybatis配置**:首先,你需要在项目的`resources`目录下创建`mybatis-config.xml`,这是Mybatis的全局配置文件,包含了数据源、事务管理器等信息。同时,还需要为每个数据库表创建对应的Mapper XML文件,用于...
2. **环境搭建**:包括如何下载MyBatis库,配置Maven或Gradle依赖,以及如何在项目中添加MyBatis的配置文件(mybatis-config.xml)。 3. **SqlSessionFactory与SqlSession**:SqlSessionFactory是创建SqlSession的...
在Mybatis-3.2.2版本中,引入了一些新特性和改进,例如增强的动态SQL支持,更完善的类型处理,以及对Java 8日期时间类型的原生支持等。这个版本还可能包含一些性能优化和bug修复,确保框架的稳定性和效率。 总的来...
3. **日志输出**:Log4j的配置至关重要。你需要在项目的配置文件(通常是log4j.properties或log4j.xml)中设置合适的日志级别(如DEBUG)、输出目的地(控制台、文件等)以及日志格式。MyBatis默认集成了Log4j,通过...
3. **MyBatis 3.2.7**:作为 3.2.x 系列的最后一个版本,此版本做了更多的改进和增强: - 添加了对Java 8日期时间API的支持,使得与新版本Java的兼容性更好。 - 优化了缓存机制,提升了多线程环境下的性能。 - ...
3. **SqlSessionFactory**:这是Mybatis的核心工厂类,用于创建SqlSession对象,进而执行SQL操作。文档会解释如何使用SqlSessionFactoryBuilder来构建SessionFactory,并介绍其生命周期管理。 4. **SqlSession**:...
这个压缩包很可能是包含了特定日期(2020年9月15日)的MyBatis插件或者与之相关的配置、示例代码或教程,旨在帮助开发者在IntelliJ IDEA中更高效地使用MyBatis。 【描述】"free-idea-mybatis2020.9.15" 简单的描述...