Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database Spring中的数据库配置以及Hibernate配置文件_Database_Spring_Hibernate - Fatal编程技术网

Database Spring中的数据库配置以及Hibernate配置文件

Database Spring中的数据库配置以及Hibernate配置文件,database,spring,hibernate,Database,Spring,Hibernate,我是Spring和Hibernate的新手。在浏览项目时,我在Hibernate和SpringXML配置文件中找到了连接url、用户名、密码等数据库详细信息。 我需要了解我们为什么这样做?您有两个选择 您可以使用hibernate管理数据库连接,也可以使用Spring管理连接 当您让Hibernate管理您的连接时,您只需要告诉Spring Hibernate配置在哪里。抱歉,我无法很容易地找到一个示例,其中Hibernate管理连接,Spring只“使用”Hibernate 另一个选项是使用S

我是Spring和Hibernate的新手。在浏览项目时,我在Hibernate和SpringXML配置文件中找到了连接url、用户名、密码等数据库详细信息。 我需要了解我们为什么这样做?

您有两个选择

您可以使用hibernate管理数据库连接,也可以使用Spring管理连接

当您让Hibernate管理您的连接时,您只需要告诉Spring Hibernate配置在哪里。抱歉,我无法很容易地找到一个示例,其中Hibernate管理连接,Spring只“使用”Hibernate

另一个选项是使用Spring管理连接和hbm文件/注释实体

下面是Spring文档中的一个片段

<beans>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="mappingResources">
            <list>
                <value>product.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.HSQLDialect
            </value>
        </property>
    </bean>

</beans>

product.hbm.xml
hibernate.dialogue=org.hibernate.dialogue.hsql方言
这里是完整的文档


除了非常具体的问题外,我在使用这两种方法时没有太大区别,但我更愿意让Spring来管理我的数据库连接,例如,我可以让Spring来处理事务,并将所有内容集中在同一个配置文件中。

是的。如果您同时使用Hibernate和Spring,那么您也可以这样尝试。 hibernate需要hibernate.cfg.xml,spring需要ApplicationContext.xml

这样做,首先创建hibernate.cfg.xml。 i、 e


com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/country
根
密码
org.hibernate.dialogue.mysqldialogue
假的
线
现在创建ApplictionContext.xml,并将hibernate.cfg.xml作为属性添加到SessionFactorybean中。 i、 e



是的,我收到了,谢谢,但是想知道为什么我们在两个配置文件中都给出了详细信息吗?我指的是目的。非常好的例子。谢谢。
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

 <session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/country</property>
    <property name="connection.username">root</property>
    <property name="connection.password">password</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">false</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <mapping class="com.hibernate.test" />

 </session-factory>

</hibernate-configuration>
    <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

      //package to scan for Annotated classes 
    <context:component-scan base-package="com.hibernate.spring" />

     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         //add and locate the hibernate.cfg.xml here
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>
  </beans>