将冬眠与春天融为一体;避免重复连接参数

时间:2013-11-09 12:51:28

标签: java spring hibernate spring-mvc

我想在春天配置hibernate ...因为我在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

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

2 个答案:

答案 0 :(得分:1)

根本不需要hibernate.cfg.xml文件。您可以将SessionFactory配置为Spring bean。

这里有一个例子:

  

持久性hibernate.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 resource="persistence-hibernate.xml"/>

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

@Repository
@Transactional
public class YourDaoImpl implements YourDao {

    @Autowired
    private SessionFactory sessionFactory;

    ...
}

答案 1 :(得分:0)

您可以使用LocalSessionFactoryBean设置属性并注入数据源,而不是使用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>