在Hibernate中是否有可能用真实值而不是问号打印生成的SQL查询?
如果无法使用Hibernate API,您会建议如何使用实际值打印查询?
在Hibernate中是否有可能用真实值而不是问号打印生成的SQL查询?
如果无法使用Hibernate API,您会建议如何使用实际值打印查询?
当前回答
日志可以工作,但并不是您想要的,但P6Spy可以完美地工作。
这里有一个简单的教程来实现以及。对我来说,这就像魅力。
下载P6Spy库:获取P6Spy -install.jar 解压:解压p6spy-install.jar文件,查找p6spy.jar和spy.properties 添加库依赖项:将p6spy.jar添加到项目库依赖项中 Modify P6Spy properties file:修改数据库配置文件。您需要用P6Spy JDBC驱动程序替换现有的JDBC驱动程序。
原来是MySQL JDBC驱动程序- (com.mysql.jdbc.Driver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
更改为P6Spy JDBC驱动程序(com.p6spy.engine.spy.P6SpyDriver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
修改P6Spy属性文件:spy.properties
用现有的MySQL JDBC驱动程序替换realdriver
realdriver=com.mysql.jdbc.Driver
# specifies another driver to use
realdriver2=
# specifies a third driver to use
realdriver3=
在logfile属性中更改日志文件的位置。所有SQL语句都将被记录到这个文件中。
窗口:
logfile=c:/spy.log
UNIX:
logfile=/srv/log/spy.log
复制的间谍。属性到项目类路径:复制间谍。属性添加到项目根文件夹。确保你的项目可以定位间谍。财产,否则就是间谍。属性文件未找到异常将被抛出。
其他回答
下面是对我有用的,在log4j.file中设置下面的属性:
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Hibernate属性设置:
hibernate.show_sql=true
日志可以工作,但并不是您想要的,但P6Spy可以完美地工作。
这里有一个简单的教程来实现以及。对我来说,这就像魅力。
下载P6Spy库:获取P6Spy -install.jar 解压:解压p6spy-install.jar文件,查找p6spy.jar和spy.properties 添加库依赖项:将p6spy.jar添加到项目库依赖项中 Modify P6Spy properties file:修改数据库配置文件。您需要用P6Spy JDBC驱动程序替换现有的JDBC驱动程序。
原来是MySQL JDBC驱动程序- (com.mysql.jdbc.Driver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
更改为P6Spy JDBC驱动程序(com.p6spy.engine.spy.P6SpyDriver)
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property> <!-- note the difference -->
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
</session-factory>
修改P6Spy属性文件:spy.properties
用现有的MySQL JDBC驱动程序替换realdriver
realdriver=com.mysql.jdbc.Driver
# specifies another driver to use
realdriver2=
# specifies a third driver to use
realdriver3=
在logfile属性中更改日志文件的位置。所有SQL语句都将被记录到这个文件中。
窗口:
logfile=c:/spy.log
UNIX:
logfile=/srv/log/spy.log
复制的间谍。属性到项目类路径:复制间谍。属性添加到项目根文件夹。确保你的项目可以定位间谍。财产,否则就是间谍。属性文件未找到异常将被抛出。
打开org.hibernate.type记录器,查看实际参数是如何绑定到问号的。
如果你正在使用Spring Boot,只需配置如下:
application.yml
logging:
level:
org.hibernate.SQL: DEBUG
org.hibernate.type: TRACE
application.properties
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
仅此而已。
你的日志会是这样的:
2020-12-07 | DEBUG | o.h.SQL:127 - insert into Employee (id, name, title, id) values (?, ?, ?, ?)
2020-12-07 | TRACE | o.h.t.d.s.BasicBinder:64 - binding parameter [1] as [VARCHAR] - [001]
2020-12-07 | TRACE | o.h.t.d.s.BasicBinder:64 - binding parameter [2] as [VARCHAR] - [John Smith]
2020-12-07 | TRACE | o.h.t.d.s.BasicBinder:52 - binding parameter [3] as [VARCHAR] - [null]
2020-12-07 | TRACE | o.h.t.d.s.BasicBinder:64 - binding parameter [4] as [BIGINT] - [1]
您需要为以下类别启用日志记录:
SQL -设置为调试,以便在执行所有SQL DML语句时记录日志 type -设置为trace以记录所有JDBC参数
所以log4j配置可以是这样的:
# logs the SQL statements
log4j.logger.org.hibernate.SQL=debug
# Logs the JDBC parameters passed to a query
log4j.logger.org.hibernate.type=trace
第一种方法相当于冬眠。Show_sql =true遗留属性时,第二个函数将打印绑定的参数。
另一种解决方案(非基于hibernate)是使用像P6Spy这样的JDBC代理驱动程序。