java - hibernate与spring的集成;避免重复连接参数

标签 java spring hibernate spring-mvc

我想在 spring 中配置 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

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

最佳答案

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

这里是一个例子:

persistence-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>

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

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

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

@Repository
@Transactional
public class YourDaoImpl implements YourDao {

    @Autowired
    private SessionFactory sessionFactory;

    ...
}

关于java - hibernate与spring的集成;避免重复连接参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19876147/

相关文章:

java - 如果 Hibernate 数据库连接失败,捕获 Grails 中的启动异常

java - 如何暂停 Runnable() 对象以进行测试

hibernate - HQL 中的日期操作

java - 黑名单 Maven 依赖项

java - 实时重新加载在 Spring boot devtools 中不起作用

java - Spring Data Neo4j 示例——如何将其采用到 Spring Data Neo4j v4.1

java - 如何以 <form :input> the value of Set that is a field of the main object of the form 打印

java - HQL - 用于分页的行标识符

java - 关于将数据保存到sql数据库的问题

java - 计算文本文件中字母的出现次数