spring多数据源配置方式

1、单数据源配置方式

<bean name="zhyDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="${zhy.url}" />
		<property name="username" value="${zhy.username}" />
		<property name="password" value="${zhy.password}" />
		<property name="filters" value="stat" />
		<property name="maxActive" value="20" />
		<property name="initialSize" value="1" />
		<property name="maxWait" value="60000" />
		<property name="minIdle" value="1" />
		<property name="timeBetweenEvictionRunsMillis" value="3000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 1 from dual" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>

<bean id="multipleDataSource" class="com.sankai.zhy.util.DynamicDataSource">
        <property name="defaultTargetDataSource" ref="zhyDataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="zhyDataSource" value-ref="zhyDataSource"/>
            </map>
        </property>

    </bean>


<!-- mybatis -->
	<bean id="zhySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="multipleDataSource" />
		<property name="configLocation"
			value="classpath:META-INF/mybatis/mybatis-config.xml" />
		<property name="mapperLocations">
			<list>
				<value>classpath:META-INF/mybatis/mapper/*Mapper.xml</value>
				<value>classpath:META-INF/mybatis/ReuseSQL.xml</value>
			</list>
		</property>
	</bean>

<!-- 配置Mybatis模版 -->
	<bean id="zhySqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="zhySqlSessionFactory" />
	</bean>

<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="zhyDataSource" />
	</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="exec*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="remove*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="minus*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<!-- <tx:method name="find*" propagation="REQUIRED" isolation="READ_COMMITTED"
				read-only="true" />
			<tx:method name="get*" propagation="REQUIRED" isolation="READ_COMMITTED"
				read-only="true" /> -->
		</tx:attributes>
	</tx:advice>

<aop:config>
		<aop:pointcut id="txPointcut"
			expression="execution(* com.sankai.zhy.service..*.*(..))" />
		<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
	</aop:config>

	<tx:annotation-driven transaction-manager="txManager" />

bd

driver=com.mysql.jdbc.Driver

zhy.url=jdbc:mysql://ip:端口/库名?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false
zhy.username=账号
zhy.password=密码

然后创建三个类

DynamicDataSource

public class DynamicDataSource extends AbstractRoutingDataSource{

	@Override
	protected Object determineCurrentLookupKey() {
		// TODO Auto-generated method stub
		 return DataSourceHolder.getDataSource();
	}

}

DataSourceHolder

public class DataSourceHolder {
	//线程本地环境

    private static final ThreadLocal<String> dataSources = new ThreadLocal<String>();

    //设置数据源

    public static void setDataSource(String customerType) {

        dataSources.set(customerType);

    }

    //获取数据源

    public static String getDataSource() {

        return (String) dataSources.get();

    }

    //清除数据源

    public static void clearDataSource() {

        dataSources.remove();

    }
}

DataSource

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
	String name() default DataSource.master;
    public static String master = "zhyDataSource";
    public static String slave1 = "pushDataSource";
}

单数据源配置完毕


下面是多数据源

其他都不变spring-datasource.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-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/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- dataSource -->

	<!-- 配置数据源 Druid --> <!-- 这里不需要配置destroy-method,因为bboss持久层在jvm退出时会自动调用数据源的close方法 -->
	<bean name="zhyDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="${zhy.url}" />
		<property name="username" value="${zhy.username}" />
		<property name="password" value="${zhy.password}" />
		<property name="filters" value="stat" />
		<property name="maxActive" value="20" />
		<property name="initialSize" value="1" />
		<property name="maxWait" value="60000" />
		<property name="minIdle" value="1" />
		<property name="timeBetweenEvictionRunsMillis" value="3000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 1 from dual" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>
	<!-- dataSource -->

<!-- dataSource -->
    <!-- 配置数据源 Druid --> <!-- 这里不需要配置destroy-method,因为bboss持久层在jvm退出时会自动调用数据源的close方法 -->
    <bean name="pushDataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${push.zhy.url}" />
        <property name="username" value="${push.zhy.username}" />
        <property name="password" value="${push.zhy.password}" />
        <property name="filters" value="stat" />
        <property name="maxActive" value="20" />
        <property name="initialSize" value="1" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="1" />
        <property name="timeBetweenEvictionRunsMillis" value="3000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 1 from dual" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
    </bean>
    <!-- dataSource -->

    <bean id="multipleDataSource" class="com.sankai.zhy.util.DynamicDataSource">
        <property name="defaultTargetDataSource" ref="zhyDataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="zhyDataSource" value-ref="zhyDataSource"/>
                <entry key="pushDataSource" value-ref="pushDataSource"/>
            </map>
        </property>

    </bean>
	<!-- mybatis -->
	<bean id="zhySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="multipleDataSource" />
		<property name="configLocation"
			value="classpath:META-INF/mybatis/mybatis-config.xml" />
		<property name="mapperLocations">
			<list>
				<value>classpath:META-INF/mybatis/mapper/*Mapper.xml</value>
				<value>classpath:META-INF/mybatis/ReuseSQL.xml</value>
			</list>
		</property>
	</bean>

	<!-- 配置Mybatis模版 -->
	<bean id="zhySqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="zhySqlSessionFactory" />
	</bean>


	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="zhyDataSource" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="exec*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="remove*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<tx:method name="minus*" propagation="REQUIRED" isolation="READ_COMMITTED" />
			<!-- <tx:method name="find*" propagation="REQUIRED" isolation="READ_COMMITTED"
				read-only="true" />
			<tx:method name="get*" propagation="REQUIRED" isolation="READ_COMMITTED"
				read-only="true" /> -->
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="txPointcut"
			expression="execution(* com.sankai.zhy.service..*.*(..))" />
		<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
	</aop:config>

	<tx:annotation-driven transaction-manager="txManager" />
	<!-- <bean id="favouriteDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
		<property name="mapperInterface" value="com.sankai.zhy.dao.FavouriteDao"/> 
		<property name="sqlSessionFactory" ref="zhySqlSessionFactory"/> </bean> <bean 
		id="articleDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property 
		name="mapperInterface" value="com.sankai.zhy.dao.ArticleDao"/> <property 
		name="sqlSessionFactory" ref="cmsSqlSessionFactory"/> </bean> -->
</beans>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值