• mybatis中使用log4j


    log4j与mybatis搭配使用

      首先,mybatis的配置文件中mybatis.xml中增加日志功能。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
      
    <configuration>
    	
    
    	<settings>
    		<!-- 开启log4j来记录日志 -->
    		<setting name="logImpl" value="log4j"/>
    	</settings>
    
    	<!-- 设置与数据库的连接信息 -->
    	<environments default="mysql">
    		<environment id="mysql">
    			<transactionManager type="jdbc"></transactionManager>
    			<dataSource type="pooled">
    				<property name="driver" value="com.mysql.jdbc.Driver"/>
    				<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
    				<property name="username" value="root"/>
    				<property name="password" value="123456"/>
    			</dataSource>
    		</environment>
    	</environments>
    	
    	<mappers>
    		<mapper resource="lixin/gan/mapper/FlowerMapper.xml"/>
    	</mappers>
    </configuration>
    

      

      现在,有FlowerMapper.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="lixin.gan.mapper.FlowerMapper">
    	<select id="selectAll" resultType="lixin.gan.pojo.Flower">
    		select * from flower
    	</select>
    	
    	<select id="selectCount" resultType="lixin.gan.pojo.Flower">
    		select count(*) from flower
    	</select>
    </mapper>
    

      

      那么,我们在log4j.properties中的配置应该是这样:

    log4j.rootCategory=error, CONSOLE,LOGFILE
    
    # 将selectAll的错误级别设置为info
    log4j.logger.lixin.gan.mapper.FlowerMapper.selectAll=info
    
    # 将selectCount的错误级别设置为debug
    log4j.logger.lixin.gan.mapper.FlowerMapper.selectCount=debug
    
    # 将lixin.gan.mapper.FlowerMapper下面的所有方法(selectAll,selectCount)都设为error
    log4j.logger.lixin.gan.mapper.FlowerMapper=error
    
    # 将所有mapper中的映射操作都设为error
    log4j.logger.lixin.gan.mapper=error
    

      

     

  • 相关阅读:
    最全的项目
    最全的liunx系统修改root用户密码
    最全docker安装步骤
    最全的ideal 常 用 的 快 捷
    最全的ideal 常用的快捷
    ftp路径
    站点路径
    sublime text3 切换中文方法
    UI设计: PS 快捷键 Ctrl+Shift+alt+T,旋转复制
    SQL中表与表的简单关系
  • 原文地址:https://www.cnblogs.com/-beyond/p/10113455.html
Copyright © 2020-2023  润新知