p6spy打印SQL


一 Springboot項目

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,(現官網打不開)

二 SSM項目

      <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.0</version>
        </dependency>

spy.properties的文件配置

# 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的配置

 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         <!-- 參考: https://blog.csdn.net/ggd628300/article/details/51758925 -->
 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事務的應用程序。其配置完成使用時,可以進行數據訪問性能的監測。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM