org.springframework.jdbc.datasource.lookup,数据库主备配置
mysql.properties:
crm.jdbc.driverClassName=com.mysql.jdbc.Driver
crm.jdbc.url=jdbc:mysql://10.100.163.77:3306/financial_sales_prod?useUnicode=true&characterEncoding=utf-8
crm.jdbc.username=telsale/-pl
crm.jdbc.password=0OKM
crm.dbcp.initialSize=5
crm.dbcp.minIdle=5
crm.dbcp.maxActive=20
crm.dbcp.maxWait=10000
crm.dbcp.timeBetweenEvictionRunsMillis=60000
crm.dbcp.minEvictableIdleTimeMillis=300000
crm.dbcp.testWhileIdle=true
crm.dbcp.testOnBorrow=true
crm.dbcp.testOnReturn=false
crm.dbcp.poolPreparedStatements=true
crm.dbcp.maxPoolPreparedStatementPerConnectionSize=20
bigdata.jdbc.driverClassName=com.mysql.jdbc.Driver
bigdata.jdbc.url=jdbc:mysql://10.100.163.78:3306/financial_sales_prod?useUnicode=true&characterEncoding=utf-8
bigdata.jdbc.username=telsale/-pl
bigdata.jdbc.password=0OKM
bigdata.dbcp.initialSize=5
bigdata.dbcp.minIdle=5
bigdata.dbcp.maxActive=20
bigdata.dbcp.maxWait=10000
bigdata.dbcp.timeBetweenEvictionRunsMillis=60000
bigdata.dbcp.minEvictableIdleTimeMillis=300000
bigdata.dbcp.testWhileIdle=true
bigdata.dbcp.testOnBorrow=true
bigdata.dbcp.testOnReturn=false
bigdata.dbcp.poolPreparedStatements=true
bigdata.dbcp.maxPoolPreparedStatementPerConnectionSize=20
applicationContext-dao.xml:
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> init-method="init" destroy-method="close"> value="${crm.dbcp.maxPoolPreparedStatementPerConnectionSize}"/> init-method="init" destroy-method="close"> value="${bigdata.dbcp.maxPoolPreparedStatementPerConnectionSize}"/> class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> rollback-for="java.lang.Exception"/> expression="execution(public * com.houbank.incoming.service.*.*(..))"/>
java:
自定义数据源:
package com.houbank.incoming.dao.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
/**
* Created by xiaogaoxu on 2016/8/25.
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DatasourceHold.getDBType();
}
@Override
public DataSource determineTargetDataSource() {
return super.determineTargetDataSource();
}
}
自定义数据源切面:
package com.houbank.incoming.dao.datasource;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
/**
* Created by xiaogaoxu on 2016/8/25.
*/
public class DynamicDataSourceAspect implements MethodBeforeAdvice, AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
DatasourceHold.clearDBType();
}
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
if (method.isAnnotationPresent(DataSource.class)) { //这里通过方法的注解区分哪些方式使用哪种数据库,大数据查询的专门使用查询的库
DataSource datasource = method.getAnnotation(DataSource.class);
DatasourceHold.setDBType(datasource.name());
} else {
DatasourceHold.clearDBType();
}
}
}
package com.houbank.incoming.dao.datasource;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {
/**
* CRM系统的主数据源 系统默认
*/
String CRM_DATASOURCE = "crm_dataSource";
/**
* 大数据的数据源 只读同步数据
*/
String BIGDATA_DATASOURCE = "bigdata_dataSource";
String name();
}
mybatis采用的是多数据源配置
crm_dataSource CRM系统的主数据源 bigdata_dataSource 大数据的数据源 只读同步数据 示例: @DataSource(name = DataSource.BIGDATA_DATASOURCE) public ListselectByCondition(TagFilterRel rel) { return tagFilterRelMapper.selectByCondition(rel); } mybatis数据源及事务配置见applicationContext-dao.xml