0

我使用的是:spring 4.2.3+MyBatis 3.4,想配置mybatis打印出sql。配置如下:

**web.xml**

<?xml version="1.0" encoding="UTF-8"?>
...
<!-- spring config -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring GWT integration -->
<servlet>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <url-pattern>/idp_web/service/*</url-pattern>
</servlet-mapping>

<!-- log4j -->
<context-param>  
    <param-name>log4jConfigLocation</param-name>  
    <param-value>WEB-INF/log4j.properties</param-value>  
</context-param>  
<context-param>  
    <param-name>log4jRefreshInterval</param-name>  
    <param-value>3000</param-value>  
</context-param>
<listener>  
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener>  

<!-- Spring Security related configuration -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

**log4j.properties**
# Global logging configuration
log4j.rootLogger=DEBUG,stdout

# Console output...
log4j.appender.stdout                            =             org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target                     = System.out
log4j.appender.stdout.Threshold                  = ALL
log4j.appender.stdout.layout                     =    org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern   = %5p [%t] - %m%n

#spring
log4j.logger.org.springframework                 = DEBUG
log4j.logger.org.spring4gwt.server               = DEBUG

# SqlMap logging configuration...
log4j.logger.com.vsi.idp.mapper                 = TRACE,stdout

#log4j.logger.org.apache=TRACE,stdout
#log4j.logger.org.mybatis=TRACE,stdout
#log4j.logger.com.ibatis=TRACE,stdout

log4j.logger.java.sql=TRACE,stdout
log4j.logger.java.sql.Connection=TRACE,stdout
log4j.logger.java.sql.Statement=TRACE,stdout
log4j.logger.java.sql.PreparedStatement=TRACE,stdout
log4j.logger.java.sql.ResultSet=TRACE,stdout

当crud时,什么都没有打印出来

我犯了什么错误?如何修复它以确保打印出 sql?

我读过这个:

http://www.mybatis.org/mybatis-3/logging.html.

只是想知道,在一个web项目中,mybatis是否会自动与spring共享相同的log4j配置?或者我应该在applicationContext.xml中添加一些配置条目以确保共享?

4

1 回答 1

0

将此添加到 ApplicationContext.xml 后,打印出 sql

<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.domain.model" />
     <!--**this line is key point**-->
    <property name="configLocation" value="WEB-INF/mybatis-config.xml"/>
</bean>

//mybatis-config.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>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
</configuration>
于 2016-04-11T07:11:06.303 回答