码迷,mamicode.com
首页 > 编程语言 > 详细

spring多数据源配置方式

时间:2016-05-06 12:52:18      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

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>



spring多数据源配置方式

标签:

原文地址:http://blog.csdn.net/u010310183/article/details/51329758

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!