设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 5318|回复: 0
打印 上一主题 下一主题

让Hibernate显示SQL语句的绑定参数值

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2013-1-17 13:42:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用Hibernate提供的内置属性<Property name="show_sql">true</Property>只能输出类似于下面的SQL语句:
  1. Hibernate:   

  2. insert into student(name, sex, age, cardId, classroom_id, id) values (?, ?, ?, ?, ?, ?)
复制代码

这样不利于程序的调试,为了可以显示?占位符所代表的具体数据,需要第三方Jar包,p6spy是一个该需求的开源实现。

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

  • 到其官方网站下载其Jar包,http://www.p6spy.com/(貌似不能访问了)靠Google或Baidu搜索一下吧。
  • 一般Download下来的是zip或jar文件包,将其解压缩,将里面的p6spy.jar放入构建路径里,将spy.properties文件放入src目 录下。
  • 修改两个属性,第一个,realdriver=你使用具体数据库的驱动类(例如MySQL为:com.mysql.jdbc.Driver);第二个,logfile=指定log文件的位置(例如,c:/spy.log)。
  • 修改Hibernate.cfg.xml配置文件中的 <property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>

配置完毕后,执行HQL语句后,查看spy.log文件就可以看到?占位符的具体数据了:(前面部分是Hibernate的初始化信息,最下面显示的就是完整的SQL语句)

  1. 1326730516837|-1||debug||com.p6spy.engine.common.P6SpyOptions reloading properties  
  2. 1326730516842|-1||info||Using properties file: D:\WorkSpace\MyEclipse\9\hibernate\list mapping\bin\spy.properties  
  3. 1326730516842|-1||info||No value in environment for: getStackTrace, using: false  
  4. 1326730516842|-1||info||No value in environment for: getAppender, using: com.p6spy.engine.logging.appender.FileLogger  
  5. 1326730516842|-1||info||No value in environment for: getDeregisterDrivers, using: false  
  6. 1326730516842|-1||info||No value in environment for: getUsePrefix, using: false  
  7. 1326730516842|-1||info||No value in environment for: getExecutionThreshold, using: 0  
  8. 1326730516842|-1||info||No value in environment for: getAutoflush, using: true  
  9. 1326730516842|-1||info||No value in environment for: getExclude, using:   
  10. 1326730516842|-1||info||No value in environment for: getExcludecategories, using: info,debug,result,batch  
  11. 1326730516843|-1||info||No value in environment for: getFilter, using: false  
  12. 1326730516843|-1||info||No value in environment for: getInclude, using:   
  13. 1326730516843|-1||info||No value in environment for: getIncludecategories, using:   
  14. 1326730516843|-1||info||No value in environment for: getLogfile, using: spy.log  
  15. 1326730516843|-1||info||No value in environment for: getRealdriver, using: com.mysql.jdbc.Driver  
  16. 1326730516843|-1||info||No value in environment for: getRealdriver2, using:   
  17. 1326730516843|-1||info||No value in environment for: getRealdriver3, using:   
  18. 1326730516843|-1||info||No value in environment for: getAppend, using: true  
  19. 1326730516843|-1||info||No value in environment for: getSpydriver, using: com.p6spy.engine.spy.P6SpyDriver  
  20. 1326730516843|-1||info||No value in environment for: getDateformat, using:   
  21. 1326730516843|-1||info||No value in environment for: getDateformatter, using: null  
  22. 1326730516843|-1||info||No value in environment for: getStringmatcher, using: com.p6spy.engine.common.SubstringMatcher  
  23. 1326730516843|-1||info||No value in environment for: getStringMatcherEngine, using: com.p6spy.engine.common.SubstringMatcher@60e128  
  24. 1326730516843|-1||info||No value in environment for: getStackTraceClass, using:   
  25. 1326730516843|-1||info||No value in environment for: getSQLExpression, using: null  
  26. 1326730516843|-1||info||No value in environment for: getReloadProperties, using: false  
  27. 1326730516843|-1||info||No value in environment for: getReloadPropertiesInterval, using: 60  
  28. 1326730516843|-1||info||No value in environment for: getJNDIContextFactory, using: null  
  29. 1326730516843|-1||info||No value in environment for: getJNDIContextProviderURL, using: null  
  30. 1326730516844|-1||info||No value in environment for: getJNDIContextCustom, using: null  
  31. 1326730516844|-1||info||No value in environment for: getRealDataSource, using: null  
  32. 1326730516844|-1||info||No value in environment for: getRealDataSourceClass, using: null  
  33. 1326730516844|-1||info||No value in environment for: getRealDataSourceProperties, using: null  
  34. 1326730517419|7|0|statement|select max(id) from classroom|select max(id) from classroom  
  35. 1326730517440|0|0|statement|select max(id) from student|select max(id) from student  
  36. 1326730517454|0|0|statement|insert into classroom (grade, number, id) values (?, ?, ?)|insert into classroom (grade, number, id) values ('fourth grade', 1, 1)  
  37. 1326730517456|1|0|statement|insert into student (name, sex, age, cardId, classroom_id, id) values (?, ?, ?, ?, ?, ?)|insert into student (name, sex, age, cardId, classroom_id, id) values ('lisi', 'true', 21, '08053120', 1, 2)  
  38. 1326730517457|0|0|statement|update student set classroom_id=?, index_=? where id=?|update student set classroom_id=1, index_=1 where id=2
  39. 1326730517458|0|0|commit||
复制代码
二、在Java Web项目中使用p6spy(Tomcat环境下)

将p6spy.jar 放入应用程序的WEB-INF/lib目录,将spy.properties放入WEB-INF/classes目录
其他与Java Project项目使用方法一样。
重启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,重新运行即可。


from:http://go.cxweb.com.cn/zikv2
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-5-2 12:23 , Processed in 0.083913 second(s), 28 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表