p6spy displays complete executable SQL

Posted by Lee W on Fri, 24 May 2019 01:14:52 +0200

Previous projects used mybtis to do ORM, it has its own SQL output, or third-party plug-ins are very good output display execution of the SQL and results. You can even use mybtis's interceptor to customize the output of formatted SQL.

The project is currently developed using hibernate 3. Projects are also relatively old... The SQL statements displayed in the console are ugly and long, and the parameters of the query are banded? No, it's really ugly... At first, I wanted to use druid connection pool to display the control of SQL, but I found that druid is not compatible with hibernate 3. Compatibility is too poor, or it did not achieve results.

Although hibernate can format SQL, it is not good enough. Here are some examples to compare:

Configuration display formatted SQL 
<prop key="hibernate.show_sql">
					true
				</prop>
				<prop key="hibernate.format_sql">true</prop> 


//Here's a comparison of consoles: p6spy is better, as you can see, and can also be used in mybtis 

Hibernate: 
    select
        smslog0_.id as id110_,
        smslog0_.content as content110_,
        smslog0_.create_time as create3_110_,
        smslog0_.phone as phone110_,
        smslog0_.return_msg as return5_110_,
        smslog0_.send_time as send6_110_,
        smslog0_.sms_no as sms7_110_,
        smslog0_.status as status110_ 
    from
        sms_log smslog0_ 
    where
        smslog0_.status=? 
    order by
        smslog0_.id asc
2017-09-19 16:30:30|0|commit|connection 26||
2017-09-19 16:30:30|43|statement|connection 27|select smslog0_.id as id110_, smslog0_.content as content110_, smslog0_.create_time as create3_110_, smslog0_.phone as phone110_, smslog0_.return_msg as return5_110_, smslog0_.send_time as send6_110_, smslog0_.sms_no as sms7_110_, smslog0_.status as status110_ from sms_log smslog0_ where smslog0_.status=? order by smslog0_.id asc
select smslog0_.id as id110_,
	smslog0_.content as content110_,
	smslog0_.create_time as create3_110_,
	smslog0_.phone as phone110_,
	smslog0_.return_msg as return5_110_,
	smslog0_.send_time as send6_110_,
	smslog0_.sms_no as sms7_110_,
	smslog0_.status as status110_
from
	sms_log smslog0_
where
	smslog0_.status=0
order by
	smslog0_.id asc


I changed to p6spy to try, according to the online Baidu out of the tutorial, and soon integrated, after testing compatible with hibernate 3. No problem. And you can customize the formatting of SQL. And so on, there are other functions. It's very convenient and easy to use. Just when integrating, put spy.properties under the root directory of classes, that is, resource s, or src. Don't throw them randomly, otherwise you can't find them, and start a wrong report. There is also the configuration path to be written correctly. It's not difficult to integrate basically without pressure. It's enough for us to just view SQL, and the rest can be left alone.

#jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.driverClassName=com.p6spy.engine.spy.P6SpyDriver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=none
#jdbc.url=jdbc\:mysql\://localhost\:3306/rpdp2p?useUnicode\=true&characterEncoding\=UTF-8
jdbc.url=jdbc:p6spy:mysql://localhost:3306/rpdp2p?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
jdbc.initialPoolSize=10
jdbc.minPoolSize=10
jdbc.maxPoolSize=100
jdbc.maxIdleTime=20
jdbc.acquireIncrement=20


Topics: JDBC Hibernate SQL MySQL