对于某些项目而言,我更喜欢使用spring data jpa(底层为hibernate),在开发和测试环境,经常需要看到sql日志,但是spring data jpa的日志打印就有点坑,总得来说就是不太好看,为了看到最终执行的sql, 只好自己动手修改了一把。
示例sql为:
@Modifying
@Query("update Article a set a.salesArticleCategoryId=:salesArticleCategoryId where a.tenantId=:tenantId and a.id in :articleList")
Integer setArticleCategoryId(Long tenantId, Long salesArticleCategoryId, List<Long> articleList);
普通打印
方法一
spring.jpa.show-sql=true
打印结果是:
update article set sales_article_category_id=? where tenant_id=? and (id in (?))
方法二
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
打印结果是
Hibernate:
update
article
set
sales_article_category_id=?
where
tenant_id=?
and (
id in (
?
)
)
打印参数
以上方法均看不到参数,需要打印参数,可以打开:
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
打印结果是
TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [1048849785215520768]
TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [474606483645009920]
TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [1052093050802933760]
增强打印
输出结果如下:
update article set sales_article_category_id=1048849785215520768 where tenant_id=474606483645009920 and (id in (1052093050802933760))