hibernate.use_sql_comments作用

本文详细介绍了如何使用Hibernate的format_sql和use_sql_comments属性来查看和增强SQL语句的可读性和可定位性,并提供了示例代码展示在控制台中自动生成的日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  hibernate.format_sql = true

如果想要仔细看Hibernate生成的SQL语句,将format_sql设为true可以很大程度地减轻痛苦……


再加上这个的话:
hibernate.use_sql_comments = true

个人认为 [可读性 + 可定位性] 会更好。

当然,这两个一起来的话也会造成SQL在console中显示的篇幅很大。例如:

Java代码 复制代码  收藏代码
  1. /**  
  2.  * 以下是Hibernate在控制台中自动生成的log  
  3.  */  
  4.   
  5. Hibernate:   
  6.     /* criteria query */ select   
  7.         this_.id as id88_0_,   
  8.         this_.flag as flag88_0_,   
  9.         this_.description as descript3_88_0_,   
  10.         this_.mobilizationDate as mobiliza4_88_0_,   
  11.         this_.recordId as recordId88_0_,   
  12.         this_.deptFromId as deptFromId88_0_,   
  13.         this_.deptToId as deptToId88_0_   
  14.     from   
  15.         Mobilization this_   
  16.     where   
  17.         this_.flag=?   
  18.     order by   
  19.         this_.id asc   
  20.   
  21. Hibernate:   
  22.     /* load net.example.model.PersonnelRecord */ select   
  23.         personnelr0_.id as id91_0_,   
  24.         personnelr0_.flag as flag91_0_,   
  25.         personnelr0_.birthDate as birthDate91_0_,   
  26.         personnelr0_.deptId as deptId91_0_,   
  27.         personnelr0_.eduQualificationId as eduQuali5_91_0_,   
  28.         personnelr0_.firstWorkDate as firstWor6_91_0_,   
  29.         personnelr0_.gender as gender91_0_,   
  30.         personnelr0_.idNumber as idNumber91_0_,   
  31.         personnelr0_.nationalityId as national9_91_0_,   
  32.         personnelr0_.originDate as originDate91_0_,   
  33.         personnelr0_.politicalConditionId as politic11_91_0_,   
  34.         personnelr0_.socialRelation as socialR12_91_0_,   
  35.         personnelr0_.description as descrip13_91_0_,   
  36.         personnelr0_.jobPositionId as jobPosi14_91_0_,   
  37.         personnelr0_.jobTitleId as jobTitleId91_0_,   
  38.         personnelr0_.jobTypeId as jobTypeId91_0_,   
  39.         personnelr0_.personName as personName91_0_,   
  40.         personnelr0_.iconUrl as iconUrl91_0_,   
  41.         personnelr0_.officialFlagId as officia19_91_0_,   
  42.         personnelr0_.marriageFlagId as marriag20_91_0_,   
  43.         personnelr0_.personnelNumber as personn21_91_0_,   
  44.         personnelr0_.quitWorkDate as quitWor22_91_0_,   
  45.         personnelr0_.address as address91_0_,   
  46.         personnelr0_.cellPhoneNumber as cellPho24_91_0_,   
  47.         personnelr0_.email as email91_0_,   
  48.         personnelr0_.phoneNumber as phoneNu26_91_0_,   
  49.         personnelr0_.nativeLocation as nativeL27_91_0_   
  50.     from   
  51.         PersonnelRecord personnelr0_   
  52.     where   
  53.         personnelr0_.id=?   
  54.   
  55. Hibernate:   
  56.     /* load net.example.model.Department */ select   
  57.         department0_.id as id81_0_,   
  58.         department0_.flag as flag81_0_,   
  59.         department0_.deptName as deptName81_0_,   
  60.         department0_.deptTypeId as deptTypeId81_0_,   
  61.         department0_.description as descript5_81_0_,   
  62.         department0_.deptNumber as deptNumber81_0_   
  63.     from   
  64.         Department department0_   
  65.     where   
  66.         department0_.id=?   
  67.   
  68. Hibernate:   
  69.     /* load net.example.model.Department */ select   
  70.         department0_.id as id81_0_,   
  71.         department0_.flag as flag81_0_,   
  72.         department0_.deptName as deptName81_0_,   
  73.         department0_.deptTypeId as deptTypeId81_0_,   
  74.         department0_.description as descript5_81_0_,   
  75.         department0_.deptNumber as deptNumber81_0_   
  76.     from   
  77.         Department department0_   
  78.     where   
  79.         department0_.id=?  
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd " > <context:property-placeholder location="classpath:application.properties"></context:property-placeholder> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.aiearth.drone.dao,com.aiearth.pojo" /> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.hbm2ddl.auto"> update </prop> --> <prop key="hibernate.dialect"> <!-- org.hibernate.spatial.dialect.postgis.PostgisDialect--> org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect <!-- org.hibernate.dialect.PostgreSQLDialect--> </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.use_sql_comments">false</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.query.escape_like_parameter">false</prop> <prop key="hibernate.query.sql.escape-identifier">false</prop> <prop key="hibernate.logger.org.hibernate.mapping">DEBUG</prop> </props> </property> </bean> <bean id="dataSource" class="com.aiearth.drone.security.DataSource.UmspscDataSource"> <property name="driverClassName" value="org.postgresql.Driver"/> <property name="jdbcUrl" value="${drone.jdbc.url}?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&stringtype=unspecified"/> <property name="username" value="${drone.jdbc.username}"/> <property name="password" value="${drone.jdbc.password}"/> <property name="connectionTimeout" value="30000"/> <property name="idleTimeout" value="180000"/> <property name="maximumPoolSize" value="30"/> <property name="minimumIdle" value="10"/> <property name="maxLifetime" value="1200000"/> <property name="connectionTestQuery" value="SELECT 1"/> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" primary="true"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.aiearth..*.service..*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> </beans>这样配对吗,如果不对请给我完整的修改后配置
最新发布
07-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值