增加SQL日志输出mybatis拦截器,并在application-global.xml中增加是否输出sql配置项
2个文件已修改
1个文件已添加
72 ■■■■■ 已修改文件
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/MyBatisConfig.java
@@ -4,6 +4,7 @@
import jakarta.annotation.PostConstruct;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -15,6 +16,8 @@
    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;
    @Value("${mybatis-plus.configuration.print-sql}")
    private boolean printSql;
    /**
     * 两个拦截器,自动生成ID,异常时输出SQL
     */
@@ -23,6 +26,9 @@
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(new AutoGenerateIdInterceptor());
            sqlSessionFactory.getConfiguration().addInterceptor(new PrintExceptionSqlInterceptor());
            if (printSql) {
                sqlSessionFactory.getConfiguration().addInterceptor(new PrintSqlInterceptor());
            }
        }
    }
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mybatis/PrintSqlInterceptor.java
New file
@@ -0,0 +1,61 @@
package com.dy.common.mybatis;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
/**
 * @Author: liurunyu
 * @Date: 2024/12/13 9:53
 * @Description
 */
/**
 * 拦截执行SQL执行场景,并将SQL打印出来
 * 拦截Executor里面的query和update方法
 */
@Intercepts({
        @Signature(
                method = "query",
                type = Executor.class,
                args = {
                        MappedStatement.class,
                        Object.class,
                        RowBounds.class,
                        ResultHandler.class
                }
        ),
        @Signature(
                type = Executor.class,
                method = "update",
                args = {
                        MappedStatement.class,
                        Object.class
                }
        )
})
@Slf4j
public class PrintSqlInterceptor  implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        if (args != null && args.length > 1) {
            MappedStatement mappedStatement = (MappedStatement) args[0];
            Object parameter = args[1];
            BoundSql boundSql = mappedStatement.getBoundSql(parameter);
            String sql = boundSql.getSql();
            log.info("\n\nSQL:\n" + sql + "\n");
            //System.out.println("SQL语句:" + sql);
        }
        return invocation.proceed();
    }
}
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -65,7 +65,8 @@
    #type-aliases-package: com.dy.pipIrrGlobal.daoBa
    configuration:
        #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #时而输出日志,时而不输出日志
        log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
        #log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl 不再应用,改为PrintSqlInterceptor拦截器输出sql日志,下面print-sql项配置
        print-sql: true # 输出sql日志开关,控制PrintSqlInterceptor拦截器是否加载
        #开启驼峰uName自动映射到u_name
        #2023-10-24经实验,下面配置true或false,都能从u_name映射到uName
        #map-underscore-to-camel-case: true
@@ -79,6 +80,8 @@
    global:
        dev: false  #是否开发阶段,true或false
        dsName: ym  #开发阶段,设置临时的数据库名称
    sql:
        print: true  #是否打印SQL语句,true或false
    nginx:
        webPort: 54321
    mw: