Mybatis- N-piece configuration log4j1, sql statement log4j2 print execution

1, log4j1 configuration

   Directory Structure:

  

   conf.xml

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE the Configuration the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" "http://mybatis.org/dtd/mybatis config.dtd--3 " > 
< configuration > 

    < Settings > 
       <-! configured to sql console output log. Mybatis low version does not support logImpl -> 
       < Setting name = "logImpl" value = "LOG4J" /> 
    </ Settings > 
    
    < Environments default = "the Oracle" > 
        < Environment the above mentioned id = "the Oracle"
            -->
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
                <property name="username" value="scott" />
                <property name="password" value="12345" />
            </dataSource>
        </environment>
        <environment id="mysql">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/world" />
                <property name="username" value="root" />
                <property name="password" value="12345" />
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <!-- 注册empMapper.xml文件-->
        <mapper resource="com/lfy/mapping/empMapper.xml"/>
    </mappers>
    
</configuration>

  log4j.properties

log4j. 1-# 
# global log configuration 
Log4j.rootLogger = ERROR, stdout
 #MyBatis logging configuration 
log4j.logger.com.lfy.mapping.empMapper = the DEBUG
 # console output 
log4j.appender.stdout = org.apache.log4j.ConsoleAppender tells that 
log4j .appender.stdout.layout = Org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern =% D [ % T ] % -5p [ % C ] -% m% n-

   empMapper.xml

<?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-->
<mapper namespace="com.lfy.mapping.empMapper">
    <select id="getEmp" parameterType="int" resultType="com.lfy.bean.Emp">
        select * from emp where empno=#{empno}
    </select>
</mapper>

   Emp.java

package com.lfy.bean;

import java.util.Date;

public class Emp {

    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hireDate;
    private Integer sal;
    private Integer comm;
    private Integer deptno;
    
    public Integer getEmpno() {
        return empno;
    }
    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public Integer getMgr() {
        return mgr;
    }
    public void setMgr(Integer mgr) {
        this.mgr = mgr;
    }
    publicDate getHiredate () {
         return hireDate; 
    } 
    Public  void setHiredate (Date hiredate) { 
        hireDate = hiredate; 
    } 
    Public Integer getSal () {
         return PS; 
    } 
    Public  void setSal (Integer PS) {
         this .sal = PS; 
    } 
    Public Integer getComm () {
         return comme; 
    } 
    Public  void setComm (Integer Comma) {
         this .comm = Comma; 
    } 
    public Integer getDeptno() {
        return deptno;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    
    @Override
    public String toString() {
        
        return "User=[empno="+empno+
                    ",ename="+ename+
                    ",job="+job+
                    ",mgr="+mgr+
                    ",hiredate="+hireDate+
                    ",sal="+sal+
                    ",comm="+comm+
                    ",deptno="+deptno+"]";     
    }
}

   MybatisTest.java

Package com.lfy.main; 

Import a java.io.InputStream; 

Import org.apache.ibatis.session.SqlSession;
 Import org.apache.ibatis.session.SqlSessionFactory;
 Import org.apache.ibatis.session.SqlSessionFactoryBuilder; 

Import COM. lfy.bean.Emp; 

/ ** 
 * configuration log using log4j-1 
 * @author LFY 
 * 
 * / 
public  class MybatisTest { 

    public  static  void main (String [] args) { 

        // MyBatis profile 
        String resource = "conf. XML " ;
         // use the class loader loads mybatis profile (which also load the associated map file)
        IS = MybatisTest the InputStream. Class .getClassLoader () the getResourceAsStream (Resource);.
         // Construction sqlSession plant 
        a SqlSessionFactory the sessionFactory = new new the SqlSessionFactoryBuilder () Build (IS);.
         // use the Resources offered load mybatis class MyBatis configuration file (which also load the associated map file)
         // Reader Reader = Resources.getResourceAsReader (Resource); 
         // build sqlSession factory
         // SqlSessionFactory = new new sessionFactory the SqlSessionFactoryBuilder () build (Reader);.
         // create a map file can be executed in the sql SQLSESSION 
        the SqlSession the session = sessionFactory.openSession ();
         / **
         * Mapping of identification string sql, 
         * com.lfy.mapping.empMapper is the value of the namespace attribute empMapper.xml file mapper label, 
         * the getEmp is the id attribute values select the tag, the id attribute value of the select tag to find SQL to execute 
         * / 
        string of Statement = "com.lfy.mapping.empMapper.getEmp"; // map the identification string sql
         // execute the query returns a unique user object sql 
        emp emp = session.selectOne (of Statement, 7839 ); 
        System.out.println (EMP); 
    } 

}

   operation result:

 

2, log4j2 Configuration

   Directory Structure:

   

  conf.xml

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE the Configuration the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" "http://mybatis.org/dtd/mybatis config.dtd--3 " > 
< configuration > 

    < Settings > 
       <-! configured to sql console output log. Mybatis low version does not support logImpl -> 
       < Setting name = "logImpl" value = "LOG4J2" /> 
    </ Settings > 
    
    < Environments default = "the Oracle" > 
        < Environment the above mentioned id = "the Oracle"
            -->
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
                <property name="username" value="scott" />
                <property name="password" value="12345" />
            </dataSource>
        </environment>
        <environment id="mysql">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/world" />
                <property name="username" value="root" />
                <property name="password" value="12345" />
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <!-- 注册empMapper.xml文件-->
        <mapper resource="com/lfy/mapping/empMapper.xml"/>
    </mappers>
    
</configuration>

   log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <!--  <File name="File" fileName="../logs/test.log" append="true">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>  -->
    </appenders>

    <loggers>
        <logger name="com.lfy.mapping" level="debug" additivity="false">
            <appender-ref ref="Console"/>
        </logger>
        <!--  <logger name="log4j.logger.java.sql.Statement" level="info" additivity="false">
            <appender-ref ref="File"/>
        </logger>
        <Logger name="com.lfy.mapping" level="debug" additivity="false">
            <AppenderRef ref="File"/>
        </Logger>  -->

        <root level="info">
            <appender-ref ref="Console"/>
            <!--  <appender-ref ref="File"/> -->
        </root>
    </loggers>
</configuration>

  operation result:

  to sum up:

    When using log4j1, introduced log4j1 need to write packet, adding the conf.xml <setting name = "logImpl" value = "LOG4J" />, the preparation of the log4j.properties;

    Log4j2 use, the need to introduce log4j2 package, adding the conf.xml <setting name = "logImpl" value = "LOG4J2" />, prepared log4j2.xml.

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11413464.html