一、简介
- 项目首 页:http://p6spy.github.io/p6spy/
- GitHub下载介绍页面:https://github.com/p6spy/p6spy/wiki/Download
- GitHub托管地址:https://github.com/p6spy/p6spy/tree/master
- 帮助文档地址:http://p6spy.readthedocs.io/en/latest/
- 帮助文档(pdf/epub)下载地址:http://download.csdn.net/detail/fanxiaobin577328725/9839326
- p6spy-3.0.0.zip下载地址:http://download.csdn.net/detail/fanxiaobin577328725/9839358
P6Spy is a framework that enables database data to be seamlessly(无缝地) intercepted(截获) and logged with no code changes to existing application. The P6Spy distribution includes P6Log, an application which logs all JDBC transactions for any Java application.
P6Spy 是针对数据库访问操作的动态监测框架(开源项目)它使得数据库数据可无缝截取和操纵,而不必对现有应用程序的代码作任何修改。P6Spy 分发包包括P6Log,它是一 个可记录任何 Java 应用程序的所有JDBC事务的应用程序。其配置完成使用时,可以进行数据访问性能的监测。
我们最需要的功能,查看sql语句,不是预编译的带问号的,而是真正的数据库执行的sql,更直观,更简单。
P6Spy是一个可以用来在应用程序中拦截和修改数据操作语句的开源框架。 通过P6Spy我们可以对SQL语句进行拦截,相当于一个SQL语句的记录器,这样我们可以用它来作相关的分析,比如性能分析。
P6SPY提供了如下几个功能:
- 记录SQL语句的执行时间戳。
- 记录SQL语句类型
- 记录SQL填入参数的和没有填入参数的SQL语句
- 根据配置的时间控制SQL语句的执行时间,对超出时间的SQL语句输出到日志文件中
p6spy.(zip/tar.gz) - This is our distribution artifact containing everything that you need to use P6Spy. you would normally download this artifact if you are installing without any code changes.
p6spy.(zip/tar.gz) -这个文件包含了你使用P6Spy所需要的任何文件。
p6spy.jar - This is the primary artifact for p6spy. If you are integrating p6spy into your application, this is the only artifact that you need.
p6spy.jar - 这个文件是p6spy的核心文件,如果你要将p6spy集成到你的应用中,你只需要这一个文件就可以了。(当然还需要一个spy.properties配置文件,我的理解就是依赖文件只需要此jar包即可)
注意:以上两个文件在Maven仓库中已经存在,所以可以直接从Maven仓库中获取到。
Maven的POM配置:
<!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.0.0</version>
</dependency>
二、实战
<1> 下载p6spy-3.0.0.zip文件,下载地址为:http://download.csdn.net/detail/fanxiaobin577328725/9839358
<2> 配置Maven依赖,如果不使用Maven可以直接将p6spy-3.0.0.zip文件中的p6spy.jar添加到lib目录中,然后Build Path -> Add To Buil Path
<3> 添加spy.properties到项目src的根目录下,该文件在p6spy-3.0.0.zip文件中
<4> 在spy.properties文件中配置真正的JDBC驱动的类名称
driverlist=com.mysql.jdbc.Driver
<5> 修改真实的JDBC的URL和驱动类,如下:
driverClassName="com.p6spy.engine.spy.P6SpyDriver"
url="jdbc:p6spy:mysql://<hostname>:<port>/<database>"
<6> 配置SQL的输出路径(以输出到控制台为例),在spy.properties文件中将appender=com.p6spy.engine.spy.appender.StdoutLogger前面的井号(#)去掉
<7> 正常运行程序,然后就可以看到预期的SQL语句了
三、配置文件详解
3.1 driverlist
This is a comma separated list of JDBC driver classes to load and re