怎样用Hibernate配置多个数据源?
想通过Hibernate访问多个数据库,不同类型的数据库,比如一个mysql,一个sqlserver,请问该怎么写配置文件?请高手给予明确解释,谢谢!...
想通过Hibernate访问多个数据库,不同类型的数据库,比如一个mysql,一个sql server,请问该怎么写配置文件?
请高手给予明确解释,谢谢! 展开
请高手给予明确解释,谢谢! 展开
3个回答
展开全部
如果用xml配置的话,那就写两个配置文件,可以不再用“hibernate.cfg.xml”做文件名,随便什么都可以,像“mysql.cfg.xml”或“sqlserver.xml”都行。用Configuration类获取SessionFactory的代码:
SessionFactory mysqlFactory = new Configuration().configure("mysql.cfg.xml").buildSessionFactory();
SessionFactory sqlserverFactory = new Configuration().configure("sqlserver.xml").buildSessionFactory();
如果你用spring,多数据库就更简单了,像这段代码可以完成所有配置:
<beans>
<bean id="mysqlDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="mysqlFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="mysqlDS"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
</bean>
<bean id="sqlserverDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:odbc:test</value>
</property>
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sqlserverFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="sqlserverDS"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
</bean>
.......
</beans>
hibernate和spring还有很多可行的配置,可以参考他们的references,有很详细地说明的。
SessionFactory mysqlFactory = new Configuration().configure("mysql.cfg.xml").buildSessionFactory();
SessionFactory sqlserverFactory = new Configuration().configure("sqlserver.xml").buildSessionFactory();
如果你用spring,多数据库就更简单了,像这段代码可以完成所有配置:
<beans>
<bean id="mysqlDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="mysqlFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="mysqlDS"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
</bean>
<bean id="sqlserverDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>jdbc:odbc:test</value>
</property>
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sqlserverFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="sqlserverDS"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test.hbm.xml</value>
</list>
</property>
</bean>
.......
</beans>
hibernate和spring还有很多可行的配置,可以参考他们的references,有很详细地说明的。
推荐于2016-03-29 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464978
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
配置数据源
<!--本地库 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/bing</value>
</property>
</bean>
<!-- 用户中心库 -->
<bean id="dataSourceUser" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/user</value>
</property>
</bean>
<!-- SessionFactory配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="${application.domain.packages.bing}"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--用户sessionFactory-->
<bean id="sessionFactoryUser" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceUser"/>
<property name="packagesToScan" value="${application.domain.packages.user}"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2005Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--事务控制-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManagerUser" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryUser" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="modify*" rollback-for="Exception"/>
<tx:method name="deploy*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<tx:advice id="txAdviceUser" transaction-manager="transactionManagerUser">
<tx:attributes>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="modify*" rollback-for="Exception"/>
<tx:method name="deploy*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
<aop:advisor advice-ref="txAdviceUser" pointcut-ref="txPointcut" />
</aop:config>
<!--本地库 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/bing</value>
</property>
</bean>
<!-- 用户中心库 -->
<bean id="dataSourceUser" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/user</value>
</property>
</bean>
<!-- SessionFactory配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="${application.domain.packages.bing}"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--用户sessionFactory-->
<bean id="sessionFactoryUser" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceUser"/>
<property name="packagesToScan" value="${application.domain.packages.user}"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2005Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--事务控制-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManagerUser" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryUser" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="modify*" rollback-for="Exception"/>
<tx:method name="deploy*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<tx:advice id="txAdviceUser" transaction-manager="transactionManagerUser">
<tx:attributes>
<tx:method name="insert*" rollback-for="Exception"/>
<tx:method name="save*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="modify*" rollback-for="Exception"/>
<tx:method name="deploy*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
<aop:advisor advice-ref="txAdviceUser" pointcut-ref="txPointcut" />
</aop:config>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
正如一些传统的经典计算机文章大都会通过一个“hello,world”的例子开始讲解一样,我们也不例外,我们也将从一个相对简单的例子来阐述Hibernate的开发方法,但如果要真正阐述Hibernate的一些重要思想,仅仅靠在屏幕上打印一些字符是远远不够的,在我们的示例程序中,我们将创建一些对象,并将其保存在数据库中,然后对它们进行更新和查询。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询