spring多数据源配置方式
2016-05-06 10:49
561 查看
1、单数据源配置方式
bd
然后创建三个类
DynamicDataSource
DataSourceHolder
DataSource
单数据源配置完毕
下面是多数据源
其他都不变spring-datasource.xml中
<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>
相关文章推荐
- Android中引入开源库(Eclipse与Android Studio)
- Springlake-02 权限&文档设置&Role设置&Folder设置&登录
- Java的深克隆与浅克隆
- Java Jersey2使用总结
- Spring核心技术(一)——IoC容器和Bean简介
- Spring 核心技术 IoC容器(一)
- JDK 源码解析 —— HashSet
- java 读取图片文件再生成新图片
- 【URLDecoder】java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in es
- ValueStack与Struts2标签-day5
- java中根据hibernate配置文件自动建表
- Java练习题
- 蓝桥杯 幂一矩阵 2014年JavaB组决赛第5题
- springMVC dao层中通过反射获取entityClass
- java I/O(一)——基本操作&&java.util.zip压缩
- Java虚拟机参数设置问题
- Java Future和Callable介绍
- Java多线程之wait(),notify(),notifyAll()
- JAVA来读取大文本文件
- Java问题汇总