【Mybatis】MyBatis之配置自定义数据源(十一)
本例是在【Mybatis】MyBatis之配置多数据源(十)的基础上进行拓展,查看本例请先学习第十章
实现原理
1、扩展Spring的AbstractRoutingDataSource抽象类(该类充当了DataSource的路由中介, 能有在运行时, 根据某种key值来动态切换到真正的DataSource上。)
从AbstractRoutingDataSource的源码中,有一个数据源查找属性(dataSourceLookup),和一个 查询数据源方法 (resolveSpecifiedDataSource):
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); protected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException {
if (dataSource instanceof DataSource) {
return (DataSource) dataSource;
}
else if (dataSource instanceof String) {
return this.dataSourceLookup.getDataSource((String) dataSource);
}
else {
throw new IllegalArgumentException(
"Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource);
}
}
2、类中数据源查找类使用的是JndiDataSourceLookup,此类查找的是JNDI数据源,参考:【Tomcat】Tomcat 配置JNDI数据源(三)
3、我们可以自定义动态数据源查找类来,来控制自定义数据源
实现案例
1、搭建项目,参考:【Mybatis】MyBatis之配置多数据源(十)
2、自定义数据源查找类
package com.test.datasource; import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import javax.sql.DataSource; import org.springframework.jdbc.datasource.lookup.DataSourceLookup;
import org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException;
import org.springframework.util.Assert; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DynamicDataSourceLookup implements DataSourceLookup { /**
* 数据源集合
*/
private final Map<String, DataSource> dataSources = new ConcurrentHashMap<>(); /**
* 根据数据源名称,获取数据源
*/
@Override
public DataSource getDataSource(String dataSourceName) throws DataSourceLookupFailureException { Assert.notNull(dataSourceName, "DataSourceName must not be null");
DataSource dataSource = this.dataSources.get(dataSourceName);
if (dataSource == null) {
// datasource not in map.. create one and add to map
dataSource = createDataSource(dataSourceName);
if(dataSource != null) {
addDataSource(dataSourceName, dataSource);
}
}
// 记录数据源状态
if(dataSource instanceof ComboPooledDataSource) {
ComboPooledDataSource ds = (ComboPooledDataSource) dataSource;
try {
System.out.println("ds.getMaxPoolSize():" + ds.getMaxPoolSize());// 最大连接数
System.out.println("ds.getMaxPoolSize():" + ds.getMaxPoolSize());// 最大连接数
System.out.println("ds.getMinPoolSize():" + ds.getMinPoolSize());// 最小连接数
System.out.println("ds.getNumBusyConnections():" + ds.getNumBusyConnections());// 正在使用连接数
System.out.println("ds.getNumIdleConnections():" + ds.getNumIdleConnections());// 空闲连接数
System.out.println("ds.getNumConnections():" + ds.getNumConnections());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 总连接数
}
return dataSource;
} /**
* 创建数据源
* @param dataSourceName
* @return
*/
private DataSource createDataSource(String dataSourceName) { ComboPooledDataSource dataSource = null; // 可根据其他业务数据,创建数据源
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("hd");
dataSource.setPassword("hd123456");
dataSource.setJdbcUrl("jdbc:mysql://mysql.naughty7878.top:3306/test_mybatis?allowPublicKeyRetrieval=true");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setDataSourceName(dataSourceName); } catch (PropertyVetoException e) {
throw new RuntimeException("Error creating data source");
}
return dataSource;
} /**
* 添加数据源到 数据源集合中
* @param dataSourceName
* @param dataSource
*/
public void addDataSource(String dataSourceName, DataSource dataSource) {
Assert.notNull(dataSourceName, "DataSourceName must not be null");
Assert.notNull(dataSource, "DataSource must not be null");
this.dataSources.put(dataSourceName, dataSource);
} /**
* 从数据源集合移除数据源
* @param dataSourceName
*/
public void removeDataSource(String dataSourceName) {
Assert.notNull(dataSourceName, "DataSourceName must not be null");
this.dataSources.remove(dataSourceName);
} }
3、编辑动态数据源类,从写determineTargetDataSource方法,查询数据源时,先从自定义数据源中查找,然后从内部数据源中查找
package com.test.datasource; import javax.sql.DataSource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /**
* 动态数据源(依赖于spring)
* @author chenheng
* @date 2019-08-03 17:27:35
*
*/
public class DynamicDataSource extends AbstractRoutingDataSource { @Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getDataSourceKey();
} @Override
protected DataSource determineTargetDataSource() { Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = null;
// 自定义外部数据源类中,查询数据源
if(lookupKey != null) {
dataSource = resolveSpecifiedDataSource(key);
}
// 在内部数据源中,查询数据源
if(dataSource == null){
dataSource= super.determineTargetDataSource();
}
return dataSource;
}
}
4、注入dynamicDataSourceLookup,并且在dataSource注入属性dataSourceLookup
<bean id="dynamicDataSourceLookup" class="com.test.datasource.DynamicDataSourceLookup">
</bean> <!-- 数据源:Spring用来控制业务逻辑。数据源、事务控制、aop -->
<bean id="dataSource" class="com.test.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="dataSource1" value-ref="dataSource1"></entry>
<entry key="dataSource2" value-ref="dataSource2"></entry>
</map>
</property>
<!-- 默认目标数据源为你主库数据源 -->
<property name="defaultTargetDataSource" ref="dataSource1"/>
<property name="dataSourceLookup" ref="dynamicDataSourceLookup"/>
</bean>
5、编辑是一个动态数据源DAO,来使用自定动态数据源
package com.test.dao; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository; @Repository
public class DynamicDao extends JdbcDaoSupport { @Autowired
public DynamicDao(DataSource dataSource) {
this.setDataSource(dataSource);
} }
6、在Service中使用
package com.test.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service; import com.test.dao.DynamicDao;
import com.test.datasource.DataSourceHolder;
import com.test.pojo.Employee; @Service
public class DynamicService { @Autowired
DynamicDao dynamicDao; public List<Employee> getEmployee() {
DataSourceHolder.setDataSourceKey("自定义");
return dynamicDao.getJdbcTemplate().query("select id, last_name lastName, gender, email from employee",
new BeanPropertyRowMapper<>(Employee.class));
} }
数据流转顺序:
1.在 DataSourceHolder中,设置数据源名称
2.Spring 调用determineCurrentLookupKey()方法<DynamicDataSource中重写AbstractRoutingDataSource类中的方法> ,从DataSourceHolder取出当前的数据库名称,并返回
3.AbstractRoutingDataSource类中determineTargetDataSource()方法调用determineCurrentLookupKey(),先匹配外部数据库,然后匹配内部数据库;
4.匹配到指定的数据库,并建立链接,即为切换到相应的数据库;
5.在指定的数据库中执行相应的sql
最新文章
- BigZhuGod的粉丝
- npm 初学者教程
- SQL 语句中union all和order by同时使用
- 在eclipse中使用第三方库总结
- html+css--水平居中总结-不定宽块状元素方法(三)
- Apache与Tomcat整合
- AS3.0面向对象的写法,类和实例
- Truncate Table user
- [Spring boot] web应用返回jsp页面
- c# 控制职能运行单一实例,再次运行显示已经运行的实例
- 全局文件 pch
- 变量 || 基本数据类型 || if、while语句
- 王磊:AI 时代物流行业的 OCR 应用
- Java内存管理及对Java对象管理
- [LeetCode] Map Sum Pairs 映射配对之和
- 爆料:2019手游折扣app是真福利还是骗人哪个靠谱?
- java虚拟机--->;>;程序计数器
- IDEA导入springboot项目不能启动
- Eclipse错误:The superclass ";javax.servlet.http.HttpServlet"; was not found on the Java Build Path
- unrecognized selector sent to class