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);  


  • 2019-02-19 10:01:55

    node下使用open模块在指定浏览器下打开url

    最近在做一个项目的过程中,得到一个远程二维码图片的url,需要扫码登录,每次都是在控制台发url打印出来,再复制粘贴到浏览器的地址栏中打开扫码,整个过程过于繁琐,于是想找一个模块,直接在node下,指定浏览器打开该图片。这样可以省不少事。

  • 2019-02-24 09:47:09

    解决Node.js的命令行输出中文乱码问题(也适用于Electron)

    ​一般我们的js文件都是试用utf8编码保存的,但是中文windows的命令行一般默认使用cp936编码(就是gbk),这样我们用js代码 console.log('中文');输出日志的时候,会发现输出的是乱码。 网上有提供一些解决方案,典型的就是用iconv或iconv-lite,把这些中文字符串先转成gbk再输出。 但是这个方案我试了多次,在win10的命令行下,utf8的字符串是成功转成了gbk字符串了(通过打印Buffer可以见到),但是输出还是乱码。

  • 2019-02-25 10:05:41

    Android Socket连接(模拟心跳包,断线重连,发送数据等)

    因为是要保证全局只能有一个连接,而且我们还需要在不同的Activity中发指令,因此肯定不能在需要发指令的界面中都去连接socket,这样一来不好管理,性能也不好,重复代码也会比较多,所以想了一下还是把socket放到service中比较好,发指令功能都放在service中即可。