p6spy可以将带参数的sql直接打出来方便调试。
1.gradle中引入
compile group: 'p6spy', name: 'p6spy', version: '3.8.1'
2.resources目录下增加spy.properties
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory# 自定义日志打印logMessageFormat=xxx.P6SpyLogger #自己实现的P6SpyLogger# 使用日志系统记录sqlappender=com.p6spy.engine.spy.appender.Slf4JLogger## 配置记录Log例外excludecategories=info,debug,result,batc,resultset# 设置使用p6spy driver来做代理deregisterdrivers=true# 日期格式dateformat=yyyy-MM-dd HH:mm:ss# 实际驱动driverlist=oracle.jdbc.OracleDriver# 是否开启慢SQL记录outagedetection=true# 慢SQL记录标准 秒outagedetectioninterval=2
3.P6SpyLogger
public class P6SpyLogger implements MessageFormattingStrategy { @Override public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) { return !"".equals(sql.trim()) ? "| took " + elapsed + "ms | " + category + " | connection " + connectionId + "\n " + P6Util.singleLine(sql) + ";" : ""; }}
4.DruidConfig
public DataSource druidDataSource() throws Exception { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName("oracle.jdbc.OracleDriver"); druidDataSource.setDbType("oracle"); ....... return new P6DataSource(druidDataSource); }