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

  

 

posted @ 2018-12-13 13:27  寻觅beyond  阅读(2032)  评论(0编辑  收藏  举报
返回顶部