MyBatis 3.2.8 + log4j2.0.2 console output SQL statement

Source: Internet
Author: User
Tags log4j

mybatis3.2.7 There is a bug, when using the Log4j2 (2.0.2) version, the class will not be found, resulting in startup failure, see

https://github.com/mybatis/mybatis-3/issues/235

But not long after, 3.2.8 has fixed this bug, the newest mybatis3.2.8 is:

Https://github.com/mybatis/mybatis-3/releases

MyBatis 3.2.8 Integration log4j2.0.2 is not complex, if you use SPRING-MVC as a web framework, here are the steps to use:

1. Pom.xml Add dependencies

1 <Dependency>2             <groupId>Org.slf4j</groupId>3             <Artifactid>Slf4j-api</Artifactid>4             <version>1.7.7</version>5         </Dependency>6         <Dependency>7             <groupId>org.apache.logging.log4j</groupId>8             <Artifactid>Log4j-api</Artifactid>9             <version>${log4j2.version}</version>Ten         </Dependency> One         <Dependency> A             <groupId>org.apache.logging.log4j</groupId> -             <Artifactid>Log4j-core</Artifactid> -             <version>${log4j2.version}</version> the         </Dependency> -         <Dependency> -             <groupId>org.apache.logging.log4j</groupId> -             <Artifactid>Log4j-web</Artifactid> +             <version>${log4j2.version}</version> -         </Dependency> +         <Dependency> A             <groupId>org.apache.logging.log4j</groupId> at             <Artifactid>Log4j-slf4j-impl</Artifactid> -             <version>2.0.2</version> -         </Dependency>
View Code

SLF4J's two items do not seem to add, you can try it yourself.

2. Add listener in Web. xml

1     <Listener>2         <Listener-class>Org.apache.logging.log4j.web.Log4jServletContextListener</Listener-class>3     </Listener>4     <Filter>5         <Filter-name>Log4jservletfilter</Filter-name>6         <Filter-class>Org.apache.logging.log4j.web.Log4jServletFilter</Filter-class>7     </Filter>8     <filter-mapping>9         <Filter-name>Log4jservletfilter</Filter-name>Ten         <Url-pattern>/*</Url-pattern> One         <Dispatcher>REQUEST</Dispatcher> A         <Dispatcher>FORWARD</Dispatcher> -         <Dispatcher>INCLUDE</Dispatcher> -         <Dispatcher>ERROR</Dispatcher> the     </filter-mapping>
View Code

3. In the Resouces directory of the MAVEN project, place the Log4j2.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <ConfigurationStatus= "Off"Monitorinterval= "1800">    3 4     <appenders>5         <Consolename= "Console"Target= "System_out">6             <Patternlayoutpattern= "%d{hh:mm:ss." SSS} [%t]%-5level%logger{36}-%msg%n " />7         </Console>        8     </appenders>9 Ten     <Loggers>             One         <Root Level= "Debug"> A             <Appenderrefref= "Console" /> -         </Root> -     </Loggers> the </Configuration>
View Code

Just tune the root logger level to the debug level

4. If the Mybatis-spring project is used to integrate the MyBatis, the spring configuration file is referenced below:

1     <BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean">2         < Propertyname= "DataSource"ref= "DataSource" />3         < Propertyname= "Configlocation"value= "Classpath:mybatis-config.xml"></ Property>4         < Propertyname= "Typealiasespackage"value= "Com.cnblogs.yjmyzz.entity"></ Property>5         < Propertyname= "Mapperlocations"value= "Classpath:mybatis/**/*.xml"></ Property>6     </Bean>
View Code

<property name= "configlocation" value= "Classpath:mybatis-config.xml" ></property> this line, the main configuration file for MyBatis is specified

5.mybatis-config configuration file

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Configuration Public "-//mybatis.org//dtd Config 3.0//en"3 "Http://mybatis.org/dtd/mybatis-3-config.dtd">4 <Configuration>5     <Settings>6         <settingname= "Logimpl"value= "Log4j2" />7     </Settings>8 </Configuration>
View Code

Specify MyBatis use LOG4J2 to log logs

If you deploy on JBoss EAP 6.x, there will be an error on startup, but it does not affect the project to run properly, this is a bug in Log4j2, which is estimated to be fixed in subsequent releases, see details: http://mail-archives.apache.org/mod_ mbox/logging-log4j-dev/201403.mbox/%[email protected]%3e

Given that it's not easy to download things on GitHub at home, finally give the Mybatis-2.3.8.jar package file download: Mybatis-3.2.8.jar.zip

MyBatis 3.2.8 + log4j2.0.2 console output SQL statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.