druid如何打印可执行sql?

druid的版本,以及日志框架使用的是最近震惊世界的log4j2-2.17.0:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

druid数据源配置中修改一下如下配置:

  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;druid.log.conn=false;druid.log.stmt=false;druid.log.rs=false;druid.log.stmt.executableSql=true

解释:
druid.log.stmt.executableSql这个配置代表开启打印可执行sql,就是sql和参数拼好的sql,拷贝出来就能执行;

出处:
com.alibaba.druid.filter.logging.LogFilter中configFromProperties方法,这个方法会执行两次,第一次从系统变量中读取配置,第二次从数据源的connectionProperties属性中读取配置:

   public void configFromProperties(Properties properties) {
        {
            String prop = properties.getProperty("druid.log.conn");
            if ("false".equals(prop)) {
                connectionLogEnabled = false;
            } else if ("true".equals(prop)) {
                connectionLogEnabled = true;
            }
        }
        {
            String prop = properties.getProperty("druid.log.stmt");
            if ("false".equals(prop)) {
                statementLogEnabled = false;
            } else if ("true".equals(prop)) {
                statementLogEnabled = true;
            }
        }
        {
            String prop = properties.getProperty("druid.log.rs");
            if ("false".equals(prop)) {
                resultSetLogEnabled = false;
            } else if ("true".equals(prop)) {
                resultSetLogEnabled = true;
            }
        }
        {
            String prop = properties.getProperty("druid.log.stmt.executableSql");
            if ("true".equals(prop)) {
                statementExecutableSqlLogEnable = true;
            } else if ("false".equals(prop)) {
                statementExecutableSqlLogEnable = false;
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_43472847/article/details/122339136