弄hibernate時,想顯示sql語句,可以設置show_sql為true來達到這個目的,但是參數值全是像PreparedStatement一樣,用?來代替的。
用p6spy可以達到顯示的那些參數原值的目的,但可讀性差。可以利用SQL Profiler來處理這個事情。
p6spy:
http://www.p6spy.com/SQL Profile:
http://www.jahia.net/jahia/page597.htmlp6spy安裝:
* 將p6spy.jar放到WEB-INF/lib目錄下,將spy.properties放到WEB-INF/classes目錄下。
* 修改你 原有 JDBC Driver為:com.p6spy.engine.spy.P6SpyDriver
* 修改 spy.properties 中的 realdriver 值為 原有 的JDBC Driver,比如我的是:com.mysql.jdbc.Driver
* 完成,運行web server。
我的日志記錄產生在 %TOMCAT_HOME%\bin下,此log位置可以能過修改 logfile??= x:\x_dir\spy.log 來控制
打開看看,看里面的日志是不是看起來比較不爽?下面我們安裝SQL Profiler來讓自已的視線爽一點。
SQL Profiler安裝:(須p6spy成功安裝)
* 將SQL Profiler自帶的spy.properties覆蓋原來的classes目錄下文件
* 修改現在spy.properties中realdriver 值為 原有 的JDBC Driver
看后看看readme注意這幾句 ^__^ :
1. Start the GUI
2. Start the webapp, in starts doing some JDBC requests we will ignore
3. Press the "reset" button on the GUI
4. Make a request to the webapp
5. Press the "pause" button after the request has finished executing
6. Press the "report" button to save profiling results as a CSV file
* 我們先用java -jar sqlprofiler.jar 運行 sql profiler
* 然后啟動web server :-)
一切盡在眼前了吧?
當然,p6spy 和 sql profiler 能做的不止這些,sql profiler還能根據你的query來幫你生成建立合適的index功能等等。