3 回答

mybatis 的springboot 集成项目已经发布了
maven
12345<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.0</version></dependency>
properties 配置
123456mybatis.config= # mybatis config filemybatis.mapperLocations= # mappers filemybatis.typeAliasesPackage= # domain object's package mybatis.typeHandlersPackage= # handler's packagemybatis.check-config-location= # check the mybatis configuration existsmybatis.executorType= # mode of execution. Default is SIMPLE

@Configuration public class MyBatisConfig {
@Value("${spring.mybatis.isShowLog}") private String isShowLog;
@Autowired private DataSource dataSource;
@Bean(name = "sqlSessionFactory") public SqlSessionFactoryBean sqlSessionFactory( ApplicationContext applicationContext) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setPlugins(new Interceptor[] { pageHelper() });
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); if("true".equalsIgnoreCase(isShowLog)){ configuration.setLogImpl(StdOutImpl.class); }
sessionFactory.setMapperLocations(applicationContext.getResources("classpath:mapper/*.xml")); return sessionFactory; }
} |
日志实现自己看着办,进去看看都有哪些实现吧

项目采用SpringMVC+Mybatis的架构,日志工具还是最常用的log4j,整合了其他框架之后,发现无法打印SQL语句,然而项目中的显示调用日志却可以正常打印出来,还有当SQL拼写有错误的时候会打印出来
然后开始看Mybatis的官方文档,关于日志这一块是怎么处理的
最近Mybatis有中文文档了,虽然不全,不过已经很好了,这里面发现了项目中存在的问题。项目中引入了shiro框架,集成了slf4j日志,导致了Mybatis无法引用log4j的配置文件打印SQL语句
问题原因:这个是Mybatis默认查找日志的顺序,自上而下,也就是说,如果项目中有前面3个日志框架时,对于Mybatis,log4j就不会生效
SLF4J
Apache Commons Logging
Log4j 2
Log4j
JDK logging
解决办法:在MyBatis的配置文件mybatis-config.xml里面添加一项setting来指定log4j
12345 | <configuration> <settings> <setting name= "logImpl" value= "LOG4J" /> </settings> </configuration> |
log4j中对指定内容进行输出,其中example为包名,可以继续细化处理
1 | log4j.logger.example=DEBUG |
添加回答
举报