让Hibernate显示SQL语句的绑定参数值
分享于 点击 38248 次 点评:13
让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:
配置完毕后,执行HQL语句后,查看spy.log文件就可以看到?占位符的具体数据了:前面部分是Hibernate的初始化信息,最下面显示的就是完整的SQL语句)
- 1326730516837|-1||debug||com.p6spy.engine.common.P6SpyOptions reloading properties
- 1326730516842|-1||info||Using properties file: D:\WorkSpace\MyEclipse\9\hibernate\list mapping\bin\spy.properties
- 1326730516842|-1||info||No value in environment for: getStackTrace, using: false
- 1326730516842|-1||info||No value in environment for: getAppender, using: com.p6spy.engine.logging.appender.FileLogger
- 1326730516842|-1||info||No value in environment for: getDeregisterDrivers, using: false
- 1326730516842|-1||info||No value in environment for: getUsePrefix, using: false
- 1326730516842|-1||info||No value in environment for: getExecutionThreshold, using: 0
- 1326730516842|-1||info||No value in environment for: getAutoflush, using: true
- 1326730516842|-1||info||No value in environment for: getExclude, using:
- 1326730516842|-1||info||No value in environment for: getExcludecategories, using: info,debug,result,batch
- 1326730516843|-1||info||No value in environment for: getFilter, using: false
- 1326730516843|-1||info||No value in environment for: getInclude, using:
- 1326730516843|-1||info||No value in environment for: getIncludecategories, using:
- 1326730516843|-1||info||No value in environment for: getLogfile, using: spy.log
- 1326730516843|-1||info||No value in environment for: getRealdriver, using: com.mysql.jdbc.Driver
- 1326730516843|-1||info||No value in environment for: getRealdriver2, using:
- 1326730516843|-1||info||No value in environment for: getRealdriver3, using:
- 1326730516843|-1||info||No value in environment for: getAppend, using: true
- 1326730516843|-1||info||No value in environment for: getSpydriver, using: com.p6spy.engine.spy.P6SpyDriver
- 1326730516843|-1||info||No value in environment for: getDateformat, using:
- 1326730516843|-1||info||No value in environment for: getDateformatter, using: null
- 1326730516843|-1||info||No value in environment for: getStringmatcher, using: com.p6spy.engine.common.SubstringMatcher
- 1326730516843|-1||info||No value in environment for: getStringMatcherEngine, using: com.p6spy.engine.common.SubstringMatcher@60e128
- 1326730516843|-1||info||No value in environment for: getStackTraceClass, using:
- 1326730516843|-1||info||No value in environment for: getSQLExpression, using: null
- 1326730516843|-1||info||No value in environment for: getReloadProperties, using: false
- 1326730516843|-1||info||No value in environment for: getReloadPropertiesInterval, using: 60
- 1326730516843|-1||info||No value in environment for: getJNDIContextFactory, using: null
- 1326730516843|-1||info||No value in environment for: getJNDIContextProviderURL, using: null
- 1326730516844|-1||info||No value in environment for: getJNDIContextCustom, using: null
- 1326730516844|-1||info||No value in environment for: getRealDataSource, using: null
- 1326730516844|-1||info||No value in environment for: getRealDataSourceClass, using: null
- 1326730516844|-1||info||No value in environment for: getRealDataSourceProperties, using: null
- 1326730517419|7|0|statement|select max(id) from classroom|select max(id) from classroom
- 1326730517440|0|0|statement|select max(id) from student|select max(id) from student
- 1326730517454|0|0|statement|insert into classroom (grade, number, id) values (?, ?, ?)|insert into classroom (grade, number, id) values ('fourth grade', 1, 1)
- 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)
- 1326730517457|0|0|statement|update student set classroom_id=?, index_=? where id=?|update student set classroom_id=1, index_=1 where id=2
- 1326730517458|0|0|commit||
二、在Java Web项目中使用p6spyTomcat环境下)
可能会出现的问题:驱动程序加载先后的问题解决
如果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,重新运行即可。
用户点评