讓Hibernate顯示SQL語句的綁定參數值

使用Hibernate提供的內置屬性<Property name="show_sql">true</Property>只能輸出類似於下面的SQL語句:Hibernate: 
   insert into student(name, sex, age, cardId, classroom_id, id) values (?, ?, ?, ?, ?, ?)

這樣不利於程序的調試,爲了可以顯示?佔位符所代表的具體數據,需要第三方Jar包,p6spy是一個該需求的開源實現。

一、在Java Project項目中使用p6spy:

  1. 到其官方網站下載其Jar包,http://www.p6spy.com/(貌似不能訪問了)靠Google或Baidu搜索一下吧。
  2. 一般Download下來的是zip或jar文件包,將其解壓縮,將裏面的p6spy.jar放入構建路徑裏,將spy.properties文件放入src目 錄下。
  3. 修改兩個屬性,第一個,realdriver=你使用具體數據庫的驅動類(例如MySQL爲:com.mysql.jdbc.Driver);第二個,logfile=指定log文件的位置(例如,c:/spy.log)。
  4. 修改Hibernate.cfg.xml配置文件中的 <property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>

  配置完畢後,執行HQL語句後,查看spy.log文件就可以看到?佔位符的具體數據了:(前面部分是Hibernate的初始化信息,最下面顯示的就是完整的SQL語句)

1347353284733|-1||debug||com.p6spy.engine.common.P6SpyOptions reloading properties
1347353284737|-1||info||Using properties file: D:\console\spring-data-jpa-examples\target\classes\spy.properties
1347353284737|-1||info||No value in environment for: getAppender, using: com.p6spy.engine.logging.appender.FileLogger
1347353284737|-1||info||No value in environment for: getDeregisterDrivers, using: true
1347353284738|-1||info||No value in environment for: getUsePrefix, using: false
1347353284738|-1||info||No value in environment for: getExecutionThreshold, using: 0
1347353284738|-1||info||No value in environment for: getAutoflush, using: true
1347353284738|-1||info||No value in environment for: getExclude, using: 
1347353284738|-1||info||No value in environment for: getExcludecategories, using: info,debug,result,batch
1347353284738|-1||info||No value in environment for: getInclude, using: 
1347353284738|-1||info||No value in environment for: getIncludecategories, using: 
1347353284738|-1||info||No value in environment for: getLogfile, using: spy.log
1347353284738|-1||info||No value in environment for: getRealdriver, using: com.mysql.jdbc.Driver
1347353284738|-1||info||No value in environment for: getRealdriver2, using: 
1347353284738|-1||info||No value in environment for: getRealdriver3, using: 
1347353284738|-1||info||No value in environment for: getAppend, using: true
1347353284738|-1||info||No value in environment for: getSpydriver, using: com.p6spy.engine.spy.P6SpyDriver
1347353284738|-1||info||No value in environment for: getDateformat, using: 
1347353284738|-1||info||No value in environment for: getDateformatter, using: null
1347353284738|-1||info||No value in environment for: getStringmatcher, using: com.p6spy.engine.common.SubstringMatcher
1347353284739|-1||info||No value in environment for: getStringMatcherEngine, using: com.p6spy.engine.common.SubstringMatcher@609959
1347353284739|-1||info||No value in environment for: getStackTraceClass, using: 
1347353284739|-1||info||No value in environment for: getSQLExpression, using: null
1347353284739|-1||info||No value in environment for: getReloadProperties, using: false
1347353284739|-1||info||No value in environment for: getReloadPropertiesInterval, using: 60
1347353284739|-1||info||No value in environment for: getJNDIContextFactory, using: null
1347353284739|-1||info||No value in environment for: getJNDIContextProviderURL, using: null
1347353284739|-1||info||No value in environment for: getJNDIContextCustom, using: null
1347353284739|-1||info||No value in environment for: getRealDataSource, using: null
1347353284739|-1||info||No value in environment for: getRealDataSourceClass, using: null
1347353284739|-1||info||No value in environment for: getRealDataSourceProperties, using: null
1347353284739|-1||info||No value in environment for: getStackTrace, using: false
1347353284739|-1||info||No value in environment for: getFilter, using: false
1347353287084|23|2|statement|/* insert org.springframework.data.jpa.example.domain.User */ insert into User (firstname, lastname, username) values (?, ?, ?)|/* insert org.springframework.data.jpa.example.domain.User */ insert into User (firstname, lastname, username) values ('張', '三', '張三')
1347353287156|32|2|rollback||

二、在Java Web項目中使用p6spy(Tomcat環境下)

  1. 將p6spy.jar 放入應用程序的WEB-INF/lib目錄,將spy.properties放入WEB-INF/classes目錄
  2. 其他與Java Project項目使用方法一樣。
  3. 重啓Tomcat服務器

可能會出現的問題:驅動程序加載先後的問題解決

  如果spy.log裏出現你的程序的數據庫驅動名稱 is a real driver in spy.properties, but it has been loaded before p6spy. p6spy will not wrap these connections. Either prevent the driver from loading, or try setting 'deregisterdrivers' to true in spy.properties.
  請把spy.properties文件裏的deregisterdrivers=false改爲deregisterdrivers=true,重新運行即可。


發佈了88 篇原創文章 · 獲贊 11 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章