1 <dependency> 2 <groupId>p6spy</groupId> 3 <artifactId>p6spy</artifactId> 4 <version>3.8.0</version> 5 </dependency>
/** * @author WGR * @create 2019/9/7 -- 12:59 */ /** * 對數據源進行封裝,打印運行sql */ @Configuration public class P6spyConfig { class P6DataSourceBeanPostProcessor implements BeanPostProcessor,Ordered { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof DataSource){ return new P6DataSource((DataSource) bean); } return bean; } @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } } @Bean public P6DataSourceBeanPostProcessor p6DataSource(){ return new P6DataSourceBeanPostProcessor(); } }
若是你是Boot項目,建議你這樣包裝數據源,在mybatisplus推薦,可是在測試過程當中沒有打印SQL,(現官網打不開)html
<dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.8.0</version> </dependency>
spy.properties的文件配置java
# P6Spy\u7684\u914d\u7f6e,\u53c2\u8003\u5b98\u65b9\u6587\u6863 # \u5b98\u65b9\u6587\u6863\u4f4d\u7f6e: http://p6spy.readthedocs.io/en/latest/configandusage.html#common-property-file-settings # \u57fa\u672c\u8bbe\u7f6e autoflush=false dateformat=yyyy-MM-dd HH:mm:ss reloadproperties=true reloadpropertiesinterval=60 # \u5b9a\u5236\u5316\u8f93\u51fa appender=com.p6spy.engine.spy.appender.Slf4JLogger logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat customLogMessageFormat=%(executionTime)ms | %(sqlSingleLine) # \u6570\u636e\u5e93\u65e5\u671f,\u5e03\u5c14\u8bbe\u7f6e databaseDialectDateFormat=yyyy-MM-dd HH:mm:ss databaseDialectBooleanFormat=boolean # JMX\u8bbe\u7f6e jmx=false # \u8fc7\u6ee4\u4e0d\u9700\u8981\u7684SQL\u8bed\u53e5 filter=true # \u6392\u9664\u7684\u8bed\u53e5\u7c7b\u578b #excludecategories=info,debug,result,resultset,batch,commit,rollback excludecategories=info,debug,result,resultset,batch,commit,rollback
xml的配置git
1 <!-- https://github.com/brettwooldridge/HikariCP --> 2 <bean id="systemDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> 3 <property name="driverClassName" value="${system.jdbc.driver}" /> 4 <property name="jdbcUrl" value="${system.jdbc.url}" /> 5 <property name="username" value="${system.jdbc.username}" /> 6 <property name="password" value="#{new String(T(java.util.Base64).getDecoder().decode('${system.jdbc.password}'.getBytes()))}" /> 7 8 9 <!-- 鏈接只讀數據庫時配置爲true, 保證安全 --> 10 <property name="readOnly" value="false" /> 11 <!-- 等待鏈接池分配鏈接的最大時長(毫秒),超過這個時長還沒可用的鏈接則發生SQLException, 缺省:30秒 --> 12 <property name="connectionTimeout" value="30000" /> 13 <!-- 一個鏈接idle狀態的最大時長(毫秒),超時則被釋放(retired),缺省:10分鐘 --> 14 <property name="idleTimeout" value="600000" /> 15 <!-- 一個鏈接的生命時長(毫秒),超時並且沒被使用則被釋放(retired),缺省:30分鐘,建議設置比數據庫超時時長少30秒,參考MySQL wait_timeout參數(show variables like '%timeout%';) --> 16 <property name="maxLifetime" value="1800000" /> 17 <!-- 鏈接池中容許的最大鏈接數。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count) --> 18 <!-- 浦發生產: 12C36G effective_spindle_count爲有效磁盤數--> 19 <property name="maximumPoolSize" value="30" /> 20 21 </bean> 22 23 <!-- 3.代理的鏈接池,爲了打印實際的SQL語句 --> 24 <bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource"> 25 <constructor-arg name="delegate" ref="systemDataSource"/> 26 </bean>
p6spy是數據庫動態監控的一種框架,它能夠使得數據庫數據無縫攔截和操做,而沒必要對現有應用程序的代碼做任何修改。P6Spy分發包包括P6Log,它是一個可記錄任何Java應用程序的全部JDBC事務的應用程序。其配置完成使用時,能夠進行數據訪問性能的監測。github