Skip to content

Commit

Permalink
💡 添加相关注释
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Sep 5, 2019
1 parent 89f1ce2 commit a93805b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public static void main(String[] args) {

@Override
public void run(String... args) {
// 设置默认的数据源
DatasourceConfigContextHolder.setDefaultDatasource();
// 查询所有数据库配置列表
List<DatasourceConfig> datasourceConfigs = configMapper.selectAll();
System.out.println("加载其余数据源配置列表: " + datasourceConfigs);
// 将数据库配置加入缓存
datasourceConfigs.forEach(config -> DatasourceConfigCache.INSTANCE.addConfig(config.getId(), config));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* <p>
* 默认数据源
* 用户标识仅可以使用默认数据源
* </p>
*
* @author yangkai.shen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public void datasourcePointcut() {
}

/**
* 前置通知 用于拦截操作
* 前置操作,拦截具体请求,获取header里的数据源id,设置线程变量里,用于后续切换数据源
*/
@Before("datasourcePointcut()")
public void doBefore(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();

// 排除不可切换数据源的方法
DefaultDatasource annotation = method.getAnnotation(DefaultDatasource.class);
if (null != annotation) {
DatasourceConfigContextHolder.setDefaultDatasource();
Expand All @@ -62,6 +63,9 @@ public void doBefore(JoinPoint joinPoint) {
}
}

/**
* 后置操作,设置回默认的数据源id
*/
@AfterReturning("datasourcePointcut()")
public void doAfter() {
DatasourceConfigContextHolder.setDefaultDatasource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @date Created in 2019/9/4 16:20
*/
@Configuration
@MapperScan(basePackages = "com.xkcoding.dynamicdatasource.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
@MapperScan(basePackages = "com.xkcoding.dynamic.datasource.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfiguration {
/**
* 创建会话工厂。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ public Connection getConnection() throws SQLException {
return datasource.getConnection();
}

/**
* 初始化数据源
* @param id 数据源id
* @return 数据源
*/
private HikariDataSource initDatasource(Long id) {
HikariDataSource dataSource = new HikariDataSource();

// 判断是否是默认数据源
if (DatasourceHolder.DEFAULT_ID.equals(id)) {
// 默认数据源根据 application.yml 配置的生成
DataSourceProperties properties = SpringUtil.getBean(DataSourceProperties.class);
dataSource.setJdbcUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
} else {
// 获取数据库的配置
// 不是默认数据源,通过缓存获取对应id的数据源的配置
DatasourceConfig datasourceConfig = DatasourceConfigCache.INSTANCE.getConfig(id);

if (datasourceConfig == null) {
Expand All @@ -55,6 +62,7 @@ private HikariDataSource initDatasource(Long id) {
dataSource.setPassword(datasourceConfig.getPassword());
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
}
// 将创建的数据源添加到数据源管理器中,绑定当前线程
DatasourceHolder.INSTANCE.addDatasource(id, dataSource);
return dataSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.xkcoding.dynamic.datasource.config.MyMapper;
import com.xkcoding.dynamic.datasource.model.User;
import org.apache.ibatis.annotations.Mapper;

/**
* <p>
Expand All @@ -11,5 +12,6 @@
* @author yangkai.shen
* @date Created in 2019/9/4 16:49
*/
@Mapper
public interface UserMapper extends MyMapper<User> {
}

0 comments on commit a93805b

Please sign in to comment.