我想在 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/