hibernate spring 连接两个数据库 如题,那位高手指导一下,怎么配置呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以配置两个数据源或者在dataSource="" 这个地方不写数据库名称 配一个sessionFactory,不同sessionFactory获取的session执行的操作 要连接多个数据库可以建2个datasource和2个sessionfactory最后在定义transactionManager的时候要用分布式的,如下:<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" > 配置两个SessionFactory,然后注入到不同的DAO,DAO要使用哪个数据库连接不要搞错了 这个很简单的啊,首先在jdbc.properties里面列出你要连接的数据库的一些信息,如url,username,password之类,当然键是不能重复的,然后xml解析的时候根据properties里面的信息就可以生成两个不同的数据源就可以了 不行呀,服务器启动没问题,但一进行操作就会报org.springframework.jdbc.BadSqlGrammarException: StatementCallback;应该还是边库的问题 我的配置文件是这样的applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> </property> <property name="url"> <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=project</value> </property> <property name="username"> <value>nw</value> </property> <property name="password"> <value>nw</value> </property> </bean> <bean id="dataSourcetext" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> </property> <property name="url"> <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=text</value> </property> <property name="username"> <value>text</value> </property> <property name="password"> <value>text</value> </property> </bean> <bean id="sessionFactorytext" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSourcetext"/> </property> <property name="mappingResources"> <list> <value>com/hitzd/operation/text/vo/Text.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.jdbc.batch_size">20</prop> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/hitzd/system/privilege/dept/vo/SYSDW.hbm.xml</value> <value>com/hitzd/system/privilege/user/vo/UserRole.hbm.xml</value> <value>com/hitzd/system/privilege/user/vo/User.hbm.xml</value> <value>com/hitzd/system/privilege/role/vo/RoleMenu.hbm.xml</value> <value>com/hitzd/system/privilege/role/vo/Role.hbm.xml</value> <value>com/hitzd/system/privilege/menu/vo/Menu.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/TZLX.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/QYJB.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/JZGM.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/JSDZ.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/HYFL.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/CodeTableEdit.hbm.xml</value> <value>com/hitzd/system/dmwh/refers/CodeNavigation.hbm.xml</value> <value>com/hitzd/system/log/vo/Log.hbm.xml</value> <value>com/hitzd/system/log/vo/Logdz.hbm.xml</value> <value>com/hitzd/system/log/vo/Loglx.hbm.xml</value> <value>com/hitzd/operation/xinxi/vo/Xxlx.hbm.xml</value> <value>com/hitzd/operation/xinxi/vo/News.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Jdbg.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Jsdz.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Mhxm.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Sb.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Sbxm.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Sbxmzl.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Tdsq.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Tzlx.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/V_ Jdbg.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Xm.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Xmjs.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Xmsp.hbm.xml</value> <value>com/hitzd/operation/mhxm/vo/Xmzl.hbm.xml</value> <value>com/hitzd/operation/xmsp/vo/v_pfxm.hbm.xml</value> <value>com/hitzd/operation/xmsp/vo/Pfxm.hbm.xml</value> <value>com/hitzd/operation/map/vo/Map.hbm.xml</value> <value>com/hitzd/operation/map/vo/MapINFO.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.jdbc.batch_size">20</prop> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="baseTransactionProxyBean" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <bean id="dbDao" parent="baseTransactionProxyBean"> <property name="target"> <bean id="dbDaoTarget" class="com.hitzd.framework.dao.DbDaoImpl"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> </property> </bean> <bean id="dbDaotext" parent="baseTransactionProxyBean"> <property name="targettext"> <bean id="dbDaoTargettext" class="com.hitzd.framework.dao.DbDaoImpl"> <property name="dataSourcetext"> <ref local="dataSourcetext"/> </property> <property name="sessionFactory"> <ref local="sessionFactorytext" /> </property> </bean> </property> </bean> <import resource="dataContext.xml"/> <import resource="logContext.xml"/> <import resource="sysdwContext.xml"/> <import resource="sysmenuContext.xml"/> <import resource="sysuserContext.xml"/> <import resource="sysroleContext.xml"/> <import resource="mhxmContext.xml"/> <import resource="pfxmContext.xml"/> <import resource="dmwhContext.xml"/> <import resource="mapContext.xml"/> <import resource="xinxiContext.xml"/> </beans> 如果需要跨库事务的话,需要ejb容器来支持jta http://www.blogjava.net/hgq0011/archive/2005/09/08/12439.html可以参考一下。 谁有 jquery +java 的web 例子 xml解析问题 一个struts+hibernate连接数据库的设计问题 求jsp实现将utf-8文本格式与ANSI文本格式相互转换 有个数据库的问题!路过的大虾,看一看呐! 如何解决新增一行记录慢的问题 100分 如何在jsp页面显示bean检索的数据??? 提高访问数据库和网页速度 JSP发送邮件的应用问题 100分求列表展开的JS代码! mysql字符串如何得到指定字符最后的位置 struts2国际化问题
或者在dataSource="" 这个地方不写数据库名称
最后在定义transactionManager的时候要用分布式的,如下:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
会报org.springframework.jdbc.BadSqlGrammarException: StatementCallback;
应该还是边库的问题
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=project</value>
</property>
<property name="username">
<value>nw</value>
</property>
<property name="password">
<value>nw</value>
</property>
</bean>
<bean id="dataSourcetext"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=text</value>
</property>
<property name="username">
<value>text</value>
</property>
<property name="password">
<value>text</value>
</property>
</bean>
<bean id="sessionFactorytext" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSourcetext"/>
</property>
<property name="mappingResources">
<list>
<value>com/hitzd/operation/text/vo/Text.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/hitzd/system/privilege/dept/vo/SYSDW.hbm.xml</value>
<value>com/hitzd/system/privilege/user/vo/UserRole.hbm.xml</value>
<value>com/hitzd/system/privilege/user/vo/User.hbm.xml</value>
<value>com/hitzd/system/privilege/role/vo/RoleMenu.hbm.xml</value>
<value>com/hitzd/system/privilege/role/vo/Role.hbm.xml</value>
<value>com/hitzd/system/privilege/menu/vo/Menu.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/TZLX.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/QYJB.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/JZGM.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/JSDZ.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/HYFL.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/CodeTableEdit.hbm.xml</value>
<value>com/hitzd/system/dmwh/refers/CodeNavigation.hbm.xml</value>
<value>com/hitzd/system/log/vo/Log.hbm.xml</value>
<value>com/hitzd/system/log/vo/Logdz.hbm.xml</value>
<value>com/hitzd/system/log/vo/Loglx.hbm.xml</value>
<value>com/hitzd/operation/xinxi/vo/Xxlx.hbm.xml</value>
<value>com/hitzd/operation/xinxi/vo/News.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Jdbg.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Jsdz.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Mhxm.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Sb.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Sbxm.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Sbxmzl.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Tdsq.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Tzlx.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/V_ Jdbg.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Xm.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Xmjs.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Xmsp.hbm.xml</value>
<value>com/hitzd/operation/mhxm/vo/Xmzl.hbm.xml</value>
<value>com/hitzd/operation/xmsp/vo/v_pfxm.hbm.xml</value>
<value>com/hitzd/operation/xmsp/vo/Pfxm.hbm.xml</value>
<value>com/hitzd/operation/map/vo/Map.hbm.xml</value>
<value>com/hitzd/operation/map/vo/MapINFO.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="baseTransactionProxyBean" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="dbDao" parent="baseTransactionProxyBean">
<property name="target">
<bean id="dbDaoTarget" class="com.hitzd.framework.dao.DbDaoImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</property>
</bean>
<bean id="dbDaotext" parent="baseTransactionProxyBean">
<property name="targettext">
<bean id="dbDaoTargettext" class="com.hitzd.framework.dao.DbDaoImpl">
<property name="dataSourcetext">
<ref local="dataSourcetext"/>
</property>
<property name="sessionFactory">
<ref local="sessionFactorytext" />
</property>
</bean>
</property>
</bean>
<import resource="dataContext.xml"/>
<import resource="logContext.xml"/>
<import resource="sysdwContext.xml"/>
<import resource="sysmenuContext.xml"/>
<import resource="sysuserContext.xml"/> <import resource="sysroleContext.xml"/>
<import resource="mhxmContext.xml"/>
<import resource="pfxmContext.xml"/>
<import resource="dmwhContext.xml"/>
<import resource="mapContext.xml"/>
<import resource="xinxiContext.xml"/>
</beans>
可以参考一下。