Druid多数据源配置

2018-01-29 15:18:57
<?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"
	xsi:schemaLocation="
					http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd 
					http://www.springframework.org/schema/tx 
					http://www.springframework.org/schema/tx/spring-tx.xsd
					http://www.springframework.org/schema/aop 
					http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 配置数据源 -->
	<bean name="datasource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- <property name="driverClassName" value="${jdbc.driver}" /> --><!-- dirverClassName 可以不加 -->
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 配置数据源1 -->
	<bean name="datasource1" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- <property name="driverClassName" value="${jdbc.driver}" /> --><!-- dirverClassName 可以不加 -->
		<property name="url" value="${jdbc.url1}"></property>
		<property name="username" value="${jdbc.username1}" />
		<property name="password" value="${jdbc.password1}" />
	</bean>

    <bean id="multipleDataSource" class="com.service.manager.util.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="datasource"/>
        <property name="targetDataSources">
            <map>
                <entry key="datasource" value-ref="datasource"/>
                <entry key="datasource1" value-ref="datasource1"/>
            </map>
        </property>
    </bean>
    
	<!--配置sqlSessionFactory 并读取mybatis的一些配置 -->
	<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="multipleDataSource"></property>
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
		<property name="mapperLocations" value="classpath*:/mybatis/*Mapper.xml" />
	</bean>

	<!-- 自动扫描 将Mapper接口生成代理注入到Spring -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.service.manager.repository.mybatis" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- 配置事物 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="multipleDataSource"></property>
	</bean>

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

	<!-- 事物的具体内容 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="append*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="repair" propagation="REQUIRED" />
			<tx:method name="delAndRepair" propagation="REQUIRED" />


			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />


			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>

	<!-- 定义一个切面,在定义的切面上加入事物 <aop:config> <aop:pointcut id="transactionPointcut" 
		expression="execution(* com.service.manager.service..*Impl.*(..))" /> <aop:advisor 
		pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> -->
</beans>

增加MultipleDataSource类(保证spring能扫描到)

package com.service.manager.util;


import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class MultipleDataSource extends AbstractRoutingDataSource {
	
	public static final String datasource = "datasource";
	public static final String datasource1 = "datasource1";
	
	private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();

    public static void setDataSourceKey(String dataSource) {
        dataSourceKey.set(dataSource);
    }

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

}

增加配置文件:

[html] view plain copy
jdbc.driver=com.mysql.jdbc.Driver  
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&autoreconnect=true&zeroDateTimeBehavior=convertToNull  
jdbc.username=root  
jdbc.password=  
  
jdbc.driver1=com.mysql.jdbc.Driver  
jdbc.url1=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&autoreconnect=true&zeroDateTimeBehavior=convertToNull  
jdbc.username1=root  
jdbc.password1=

Controller切换数据源:

  1. MultipleDataSource.setDataSourceKey(MultipleDataSource.datasource);  


  • 2017-04-15 21:49:06

    Android样式的开发:Style篇

    前面铺垫了那么多,终于要讲到本系列的终篇,整合所有资源,定义成统一的样式。 哪些该定义成统一的样式呢?举几个例子吧:

  • 2017-04-15 23:54:49

    ViewPager+Fragment取消预加载以及禁止滑动

    取消预加载 网上了解了很多取消预加载的方法,里面提到了使用一个viewpager的public方法setOffscreenPageLimit 经过查看源码以及验证发现该方法是管理Viewpager预加载的页数,最低也是默认为一页(例如ViewPager一共有4页,当前手机屏幕显示第一页

  • 2017-04-15 23:56:30

    onInterceptTouchEvent和onTouchEvent调用关系详解

    如果没有onInterceptTouchEvent,只考虑onTouchEvent的话,比较容易分析和理解。假如有三层布局结构,linearLayout1,linearLayout2,textView,从前到后是包含的关系。那么下面分情况说明。

  • 2017-04-16 19:36:32

    ViewPager预加载问题和onCreateView多次调用问题的解决

    1,在使用ViewPager嵌套Fragment的时候,由于VIewPager的几个Adapter的设置来说,都会有一定的预加载(默认是左右各一个Frament)。通过设置setOffscreenPageLimit(int number) 来设置预加载的熟练,在V4包中,默认的预加载是1,即使你设置为0,也是不起作用的,设置的只能是大于1才会有效果的。我们需要通过更改V4包中的默认属性才可以

  • 2017-04-16 21:02:55

    ImageView的android:adjustViewBounds属性

    取值为true时: Adjust the ImageView's bounds to preserve the aspect ration of its drawable. 调整ImageView的界限来保持图像纵横比不变。 这并不意味着ImageView的纵横比就一定和图像的纵横比相同

  • 2017-04-18 17:12:50

    Laravel 读取 config 下的数据

    Laravel的config下一般存放配置信息,可以通过config('key')方法获取指定的数据。 设置值可通过「点」式语法读取,其中包含要访问的文件名以及选项名称。