druid 打印 sql
关注:
SimpleJpaRepository
org.hibernate.internal.SessionImpl
DruidPooledPreparedStatement
com.alibaba.druid.filter.logging.LogFilter
private void logExecutableSql(StatementProxy statement, String sql) { if (this.isStatementExecutableSqlLogEnable()) { int parametersSize = statement.getParametersSize(); if (parametersSize == 0) { this.statementLog("{conn-" + statement.getConnectionProxy().getId() + ", " + this.stmtId(statement) + "} executed. \n" + sql); } else { List<Object> parameters = new ArrayList(parametersSize); for(int i = 0; i < parametersSize; ++i) { JdbcParameter jdbcParam = statement.getParameter(i); parameters.add(jdbcParam.getValue()); } String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType(); String formattedSql = SQLUtils.format(sql, dbType, parameters); this.statementLog("{conn-" + statement.getConnectionProxy().getId() + ", " + this.stmtId(statement) + "} executed. \n" + formattedSql); } } }
druid 打印 sql:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > <!--主数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close" primary="true" > <!-- 基本属性 url、user、password --> <property name="url" value="${datasource.url}" /> <property name="username" value="${datasource.username}" /> <property name="password" value="${datasource.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10" /> <property name="minIdle" value="10" /> <property name="maxActive" value="20" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 1 " /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 --> <property name="filters" value="stat,wall,log4j"/> <property name="proxyFilters"> <list> <ref bean="log-filter"/> </list> </property> </bean> <bean id="log-filter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> <property name="connectionLogEnabled" value="true"/> <property name="statementLogEnabled" value="true"/> <property name="resultSetLogEnabled" value="true"/> <property name="statementExecutableSqlLogEnable" value="true"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
springboot 的application.properties 中配置
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.logSlowSql=true;druid.stat.slowSqlMillis=5000;druid.log.stmt.executableSql=true;druid.log.stmt=true;druid.log.rs=true;druid.log.stmt.executableSql=true
参考:
com.alibaba.druid.filter.logging.LogFilter
public LogFilter(){
configFromProperties(System.getProperties());
}