转:Spring多数据源XML配置
文章目录
1 Spring中XML配置多数据源
1.1 传统方式
配置文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<!--对应数据A的数据源-->
<bean id="dataSource_A" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${A.driver_class}" />
<property name="url" value="${A.url}" />
<property name="username" value="${A.username}" />
<property name="password" value="${A.password}" />
</bean>
<!--对应数据库B的数据源-->
<bean id="dataSource_B" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${B.driver_class}" />
<property name="url" value="${B.url}" />
<property name="username" value="${B.username}" />
<property name="password" value="${B.password}" />
</bean>
<!-- A的sessionFactory -->
<bean id="sessionFactory_A" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource_A"/>
</bean>
<!-- B的sessionFactory -->
<bean id="sessionFactory_B" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource_B"/>
</bean>
<!--使用A数据库的DAO-->
<bean id = "XDao" class = "xxx.xxx.xDaoImpl">
<property name="sessionFactory" ref="sessionFactory_A"></property>
</bean>
<!--使用B数据库的DAO-->
<bean id = "XDao" class = "xxx.xxx.xDaoImpl">
<property name="sessionFactory" ref="sessionFactory_B"></property>
</bean>
1.2 使用AbstractRoutingDataSource实现
由虚拟的DataSource
根据Client提供的上下文来实现数据源的选择。
具体的实现就是,虚拟的DataSource
仅需继承AbstractRoutingDataSource
实现determineCurrentLookupKey()
在其中封装数据源的选择逻辑
public class DataSourceConst {
public static final String Admin="12";
public static final String User = "123";
}
//建立一个获得和设置上下文环境的类,主要负责改变上下文数据源的名称
public class DataSourceContextHolder {
private static final ThreadLocal contextHolder = new ThreadLocal(); // 线程本地环境
// 设置数据源类型
public static void setDataSourceType(String dataSourceType) { contextHolder.set(dataSourceType);
}
// 获取数据源类型
public static String getDataSourceType() {
return (String) contextHolder.get();
}
// 清除数据源类型
public static void clearDataSourceType () {
contextHolder.remove();
}
}
//建立动态数据源类,注意,这个类必须继承AbstractRoutingDataSource,且实现方法
//determineCurrentLookupKey,该方法返回一个Object,一般是返回字符串
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceType();
}
}
编写spring的配置文件配置多个数据源
<!-- 数据源相同的内容 -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="parentDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<!-- 数据库test -->
<bean parent="parentDataSource" id="adminDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
</bean>
<!-- 数据库test1 -->
<bean parent="parentDataSource" id="userDataSource">
<property name="url">
<value>jdbc:mysql://localhost:3306/test2</value>
</property>
</bean>
<!-- 编写spring 配置文件的配置多数源映射关系 -->
<bean class="com.frogking.datasource.DynamicDataSource"
id="dataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="adminDataSource" key="12"></entry>
<entry value-ref="userDataSource" key="123"></entry>
</map>
</property>
<property name="defaultTargetDataSource"
ref="adminDataSource">
</property>
</bean>
<!-- sessionFactory的配置 -->
<bean
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
id="sessionFactory">
<property name="dataSource">
<ref local="dataSource"></ref>
</property>
<!-- 实体映射资源 -->
<property name="mappingResources">
<list>
<value>com/frogking/entity/User.hbm.xml</value>
<value>com/frogking/entity/Admin.hbm.xml</value>
</list>
</property>
<!-- 为sessionFactory配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sq">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop
key="hibernate.bytecode.use_reflection_optimizer">
true
</prop>
</props>
</property>
</bean>
<bean id="userDao" class="com.frogking.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
删除Hibernate中表的配置文件catalog,他会自动匹配数据库如果指定catalog匹配将不启用
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.frogking.entity.Admin" table="admin" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="100" />
</property>
<property name="passwrod" type="java.lang.String">
<column name="passwrod" length="100" />
</property>
</class>
</hibernate-mapping>