我找到了一种方法,投入让其他人也可以受益
为了记录 sql 语句,请下载 Simple Logging Facade for Java(在此处下载 slf4j)
在我的类路径中添加了以下内容,除了常规的 mybatis、odbc 和 oracle jar
- log4j-xxxx.jar
- log4j-over-slf4j-xxxx.jar
- log4j-滚动-appender.jar
- slf4j-api-xxxx.jar
- slf-log4j12-xxxx.jar
注意:这里 xxxx 是合适的版本
并在我的 log4j 中附加以下几行(请参阅我的问题)
# logger debug
log4j.logger.test.Log4jTestMyBatis=DEBUG, convert
log4j.appender.convert = org.apache.log4j.ConsoleAppender
log4j.appender.convert.layout=org.apache.log4j.PatternLayout
log4j.appender.convert.layout.ConversionPattern=[%d{HH:mm:ss}] %-5p %c{3} %x - %m%n
# end logger debug
# mybatis loggers #
log4j.logger.com.ibatis=DEBUG, convert
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG, convert
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG, convert
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG, convert
这是我用于测试的 Groovy 类示例
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.PropertyConfigurator;
import com.abc.db.ConfigInfo;
import com.abc.db.ConfigInfoExample;
import com.abc.db.client.ConfigInfoMapper;
import com.abc.db.init.DatabaseConnectivity;
class Log4jTestMyBatis {
static Logger logger = LoggerFactory.getLogger(Log4jTestMyBatis.class)
static main(args) {
PropertyConfigurator.configure(Log4jTestMyBatis.class.getResource("log4j.properties"));
DatabaseConnectivity.init()
SqlSession newABCSession = DatabaseConnectivity.getNewABCSessionFactory().openSession()
ConfigInfoMapper mapper = newABCSession.getMapper(ConfigInfoMapper.class)
ConfigInfoExample qExample = new ConfigInfoExample()
qExample.createCriteria().andProjectIdEqualTo("0-12170")
List<ConfigInfo> ctlist = mapper.selectByExample(qExample)
logger.debug(ctlist.get(0).getCfgName())
newABCSession.close()
logger.debug("debug")
}
}