标签:
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" />
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(); } }
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(); } }
@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>
标签:
原文地址:http://blog.csdn.net/u010310183/article/details/51329758