SpringBoot整合MyBatis和MyBatis-Plus请求后不打印sql日志的问题解决
问题发现
在整合springBoot+myBatis时,发现请求不打印sql日志,示例代码如下:
@RestController
public class MyController {
@Autowired
ProductMapper productMapper;
@GetMapping("/test")
public void test() {
System.out.println("进来了");
productMapper.list();
System.out.println("执行完毕");
}
}
@Mapper
public interface ProductMapper {
List<Product> list();
}
<?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="com.example.demo.mapper.ProductMapper">
<resultMap id="BaseResultMap" type="com.example.demo.domain.Product">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="productName" column="product_name" jdbcType="VARCHAR"/>
<result property="number" column="number" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id,product_name,number
</sql>
<select id="list" resultMap="BaseResultMap">
select * from product
</select>
</mapper>
执行结果如图:

问题解决
然后使用本地main方法访问SqlSession的时候又可以打印日志
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Test {
public static void main(String[] args) throws IOException {
// 创建配置文件输入流
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 根据配置文件创建 SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try(SqlSession sqlSession = sqlSessionFactory.openSession();) {
ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
mapper.list();
}
}
}
执行结果如图

百度后发现,本地访问通过加载mybatis-config.xml,会默认打印日志。
springBoot需要手动开启日志才行,具体配置如下:
#默认使用 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
如果你使用log4j,需要创建log4j. properties文件:
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
#定义日志输出的格式模式
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/main.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
#定义日志输出的格式模式
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
执行结果如图

如果你使用的是MyBatis-Plus,也需要手动开启日志,配置如下:
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
到此这篇关于SpringBoot整合MyBatis和MyBatis-Plus请求后不打印sql日志的问题解决的文章就介绍到这了,更多相关SpringBoot不打印sql日志内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot实现Read Through模式的操作过程
Read Through模式通常是指一种缓存策略,其中当应用程序尝试读取数据时,缓存系统首先被检查以查看数据是否已经存在于缓存中,这篇文章主要介绍了SpringBoot实现Read Through模式,需要的朋友可以参考下2024-07-07
SpringBoot返回Json对象报错(返回对象为空{})
本文主要介绍介绍了SpringBoot返回Json对象报错(返回对象为空{}),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-01-01
详解全局事务注解@GlobalTransactional的识别
这篇文章主要为大家介绍了详解全局事务注解@GlobalTransactional的识别源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-12-12
jasypt SaltGenerator接口定义方法源码解读
这篇文章主要为大家介绍了jasypt SaltGenerator接口定义方法源码解读,,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-09-09


最新评论