0

我想在春天配置休眠...为此我正在使用以下配置spring-servlet.xml

<context:property-placeholder location="classpath:resources/database.properties" />
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <property name="driverClassName" value="${database.driver}"></property>
        <property name="url" value="${database.url}"></property>
        <property name="username" value="${database.user}"></property>
        <property name="password" value="${database.password}"></property>
      </bean>

这是我的 database.properties 文件

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://10.2.5.142:3306/testdb
database.user=root
database.password=
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

现在我只想使用 SessionFactory 的单个实例;为此,我在我的 DAO 类中包含了以下代码

SessionFactory sessionFactory = new Configuration().configure("/resources/hibernate.cfg.xml").buildSessionFactory(); 

我必须在两个地方设置数据库相关参数

(1) database.properties 
(2) hibernate.cfg.xml

有什么方法可以让我只将这些值放在一个地方

4

2 回答 2

1

您根本不需要该hibernate.cfg.xml文件。您可以将您的配置SessionFactorySpringbean。

这是一个例子:

持久性休眠.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:resources/database.properties"/>

    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="${database.driver}"
          p:url="${database.url}"
          p:username="${database.user}"
          p:password="${database.password}" />

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:packagesToScan="package.with.your.entities">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="txnManager"/>

    <bean id="txnManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory"/>

</beans>

您可以spring-servlet.xml使用 import 标记将该配置导入您的配置中。

<import resource="persistence-hibernate.xml"/>

然后,注入您的 SessionFactory 而不是自己实例化它。

@Repository
@Transactional
public class YourDaoImpl implements YourDao {

    @Autowired
    private SessionFactory sessionFactory;

    ...
}
于 2013-11-09T13:11:59.787 回答
0

您可以使用 aLocalSessionFactoryBean设置属性并注入 DataSource 而不是 using hibernate.cfg.xml,请参阅http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/htmlsingle/#orm-hibernate

这是 Spring Reference 中的一个示例:

  <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>
于 2013-11-09T13:07:54.720 回答