当前位置: 首页 > 面试题库 >

将Hibernate SQL显示到控制台(春季)

陈和裕
2023-03-14
问题内容

我正在使用Spring 3,Hibernate4。我正在尝试遵循本教程http://www.mkyong.com/hibernate/hibernate-
display-generation-sql-to-console-show_sql-format_sql-and-use_sql_comments
/

,但是我的hibernate配置不同:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">


  <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with 
       username root and blank password. Change below if it's not the case -->
 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/project"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    <property name="validationQuery" value="SELECT 1"/>
     <property name="show_sql" value="true" />
    <property name="format_sql" value="true" />
    <property name="use_sql_comments" value="true" />
  </bean>


</beans>

而且属性show_sql,format_sql和use_sql_comments不能通过这种方式工作。我得到这个例外:

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'show_sql' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'show_sql' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

无论如何,有没有用bean的定义来实现本教程?


问题答案:

show_sql不是org.apache.commons.dbcp.BasicDataSource的属性。您必须在会话工厂配置中定义它。像这样

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="data" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>


 类似资料:
  • 当然,对于GUI,除非运行我上面提到的命令,否则不会加载控制台,所以如果您有很多打印语句,它是否仍然影响程序的速度,它们是否仍然打印?(即使认为没有显示终端/控制台)

  • 问题内容: 在Java中,我尝试将String写入控制台。字符串的长度为20166个字符。将字符串打印到控制台后,仅出现字符串的后半部分。 整个字符串是一长行: 看起来像什么: 从一开始就有很多空格(应该是字母数字字符),然后是其余的字符串。 我试图将控制台编码从默认更改为UTF-16和UTF-8,但这没有帮助。 我要输出的字符串是从特定网页(http://docs.jquery.com/Tuto

  • 问题内容: 从Python控制台显示或图片的最简单方法是什么? 我有一个Python控制台程序正在检查一个数据集,该数据集包含到本地存储的图像的链接。我应该如何编写脚本以便显示图像弹出的图形窗口? 问题答案: 使用很棒的枕头库: 这将在默认图像查看器中打开图像。

  • 我在SpringBoot api上工作,并使用具有以下属性设置的H2数据库。 当我想使用浏览器通过'http://localhost:8082/h2-console'查看H2数据库控制台时,浏览器中打开了一个带有连接和测试连接按钮的屏幕。当我单击Test Connection时,它返回成功,但当单击Connect按钮时,出现错误,即localhost拒绝连接。

  • 问题内容: 我正在Python控制台中进行大量工作,其中大部分是指地址,我希望以十六进制形式查看。 因此,如果我只是进入控制台以查看其值时,我希望python用代替。 我知道我可以输入以十六进制形式查看它,但是我正在寻找某种python控制台选项来使其自动执行此操作。像这样的东西存在吗?谢谢! 问题答案: 常规Python解释器将调用来真正显示您输入的表达式。您可以将其替换为可以完全显示所需内容的

  • 之后,什么都不显示。知道怎么解决吗?