zoukankan      html  css  js  c++  java
  • 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的文件配置

    # P6Spyu7684u914du7f6e,u53c2u8003u5b98u65b9u6587u6863
    # u5b98u65b9u6587u6863u4f4du7f6e: http://p6spy.readthedocs.io/en/latest/configandusage.html#common-property-file-settings
    
    # u57fau672cu8bbeu7f6e
    autoflush=false
    dateformat=yyyy-MM-dd HH:mm:ss
    reloadproperties=true
    reloadpropertiesinterval=60
    
    # u5b9au5236u5316u8f93u51fa
    appender=com.p6spy.engine.spy.appender.Slf4JLogger
    logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
    customLogMessageFormat=%(executionTime)ms | %(sqlSingleLine)
    
    # u6570u636eu5e93u65e5u671f,u5e03u5c14u8bbeu7f6e
    databaseDialectDateFormat=yyyy-MM-dd HH:mm:ss
    databaseDialectBooleanFormat=boolean
    
    # JMXu8bbeu7f6e
    jmx=false
    
    # u8fc7u6ee4u4e0du9700u8981u7684SQLu8bedu53e5
    filter=true
    
    # u6392u9664u7684u8bedu53e5u7c7bu578b
    #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事务的应用程序。其配置完成使用时,可以进行数据访问性能的监测。

  • 相关阅读:
    LeetCode 32. 最长有效括号(Longest Valid Parentheses)
    LeetCode 141. 环形链表(Linked List Cycle)
    LeetCode 160. 相交链表(Intersection of Two Linked Lists)
    LeetCode 112. 路径总和(Path Sum)
    LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)
    LightGBM新特性总结
    sql service 事务与锁
    C#泛型实例详解
    C# 中的委托和事件(详解)
    C# DateTime日期格式化
  • 原文地址:https://www.cnblogs.com/dalianpai/p/11677998.html
Copyright © 2011-2022 走看看