解决SpringBoot整合MyBatis和MyBatis-Plus,请求后不打印sql日志 ...

  论坛元老 | 2024-8-3 16:43:11 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1744|帖子 1744|积分 5232

标题发现

在整合springBoot+myBatis时,发现请求不打印sql日志,示例代码如下:
  1. @RestController
  2. public class MyController {
  3.     @Autowired
  4.     ProductMapper productMapper;
  5.     @GetMapping("/test")
  6.     public void test() {
  7.         System.out.println("进来了");
  8.         productMapper.list();
  9.         System.out.println("执行完毕");
  10.     }
  11. }
  12. @Mapper
  13. public interface ProductMapper {
  14.     List<Product> list();
  15. }
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3.         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.demo.mapper.ProductMapper">
  6.     <resultMap id="BaseResultMap" type="com.example.demo.domain.Product">
  7.             <id property="id" column="id" jdbcType="INTEGER"/>
  8.             <result property="productName" column="product_name" jdbcType="VARCHAR"/>
  9.             <result property="number" column="number" jdbcType="INTEGER"/>
  10.     </resultMap>
  11.     <sql id="Base_Column_List">
  12.         id,product_name,number
  13.     </sql>
  14.     <select id="list" resultMap="BaseResultMap">
  15.         select * from product
  16.     </select>
  17. </mapper>
复制代码
实行结果如图:

标题解决

然后使用本地main方法访问SqlSession的时间又可以打印日志
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
  4. public class Test {
  5.     public static void main(String[] args) throws IOException {
  6.         // 创建配置文件输入流
  7.         InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  8.         // 根据配置文件创建 SqlSessionFactory
  9.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  10.         try(SqlSession sqlSession = sqlSessionFactory.openSession();) {
  11.             ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
  12.             mapper.list();
  13.         }
  14.     }
  15. }
复制代码
实行结果如图

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

如果你使用的是MyBatis-Plus,也必要手动开启日志,配置如下:
  1. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.log4j.Log4jImpl
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表