SSH框架操作多数据库 (补16年12月)

背景:
        项目库是A库,项目中某个模块需要获取B库的数据
环境:
        MyEclipse2014  jdk1.6.0_45     tomcat6
要点:
        1.配置文件结构

        2.bean的id


1.配置文件结构目录

    manager对多个service进行管理,auth是单独出来的权限配置




2. applicationContext_Adata.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-2.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<!-- <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> -->
			<!-- <value>com.mysql.jdbc.Driver</value> -->
			<!-- <value>oracle.jdbc.driver.OracleDriver</value> -->
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<!-- <value>jdbc:sqlserver://localhost:1433;DatabaseName=frame</value>  -->
			<!-- <value>jdbc:mysql://localhost:3306/frame?useOldAliasMetadataBehavior=true</value> -->
			<!-- <value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=localhost)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> -->
			<!-- <value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> -->
			<value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value>
<!-- 			<value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> -->
		</property>
		<property name="username">
			<value>***</value>
			<!-- <value>system</value> -->
		</property>
		<property name="password">
			<value>***</value>
			<!-- <value>123456</value> -->
		</property>
		<property name="maxActive"> 
        	<value>1000 </value> 
        </property> 
        <property name="maxIdle"> 
        	<value>500 </value> 
        </property> 
        <property name="maxWait"> 
        	<value>5000 </value> 
        </property>
        <!-- 
        <property name="validationQuery"> 
        <value>select 1 from dual </value> 
        </property> 
       	  -->
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					<!-- org.hibernate.dialect.SQLServerDialect -->
					<!-- org.hibernate.dialect.MySQLDialect -->
					<!-- org.hibernate.dialect.Oracle9Dialect -->
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
			
				<!-- 这里放hibernate的映射文件 -->
				<value></value>
					
			</list>
		</property>
	</bean>
	
	<!-- transaction configration start -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
		<property name="nestedTransactionAllowed">
			<value>true</value>
		</property>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="bulkUpdate*" propagation="REQUIRED"/>
			<tx:method name="merge*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="getSave*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!-- transaction configration end -->
	<tx:annotation-driven/>
	
	<bean id="hibernateDao" class="com.djzh.dao.impl.HibernateDAO" scope="prototype">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<bean id="manager" class="com.djzh.biz.impl.AbstractManager">
		<property name="dao">
			<ref bean="hibernateDao"/>
		</property>
	</bean>
	
	<bean id="applicationContextUtil" class="com.djzh.common.util.ApplicationContextUtil">
	</bean>
	
</beans>


3.applicationContext_Bdata.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-2.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="rockDataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value>
		</property>
		<property name="username">
			<value>***</value>
		</property>
		<property name="password">
			<value>***</value>
		</property>
		<property name="maxActive"> 
        	<value>1000 </value> 
        </property> 
        <property name="maxIdle"> 
        	<value>500 </value> 
        </property> 
        <property name="maxWait"> 
        	<value>5000 </value> 
        </property>
	</bean>
	<bean id="rockSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="rockDataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				
				<!-- 这里放hibernate的映射文件 -->
				<value></value>
				
			</list>
		</property>
	</bean>
	
	<bean id="rockTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="rockSessionFactory" />
		</property>
		<property name="nestedTransactionAllowed">
			<value>true</value>
		</property>
	</bean>

	<tx:annotation-driven/>
	
	<bean id="rockHibernateDao" class="com.djzh.dao.impl.HibernateDAO" scope="prototype">
		<property name="sessionFactory">
			<ref bean="rockSessionFactory" />
		</property>
	</bean>
	
	<bean id="rockManager" class="com.djzh.biz.impl.AbstractManager">
		<property name="dao">
			<ref bean="rockHibernateDao"/>
		</property>
	</bean>

</beans>


4. 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-2.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<import resource="applicationContext_auth.xml" />
	<import resource="applicationContext_Adata.xml" />
	<import resource="applicationContext_Bdata.xml" />
	<import resource="applicationContext_service.xml" />
	<import resource="applicationContext_manager.xml" />
	<import resource="applicationContext_action.xml" />
	
</beans>


5. ApplicationContextUtil类


package com.zlwy.common.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextUtil implements ApplicationContextAware {
	
	private  static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.context=applicationContext;
	}
	public static ApplicationContext getContext() {
		return context;
	}

}


希望对有需要的朋友一点儿帮助!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值