xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceJdbcProvider.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,91 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill åº„骞 (smallchill@163.com)
 */
package org.springblade.core.tenant.dynamic;
import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springblade.core.tool.utils.StringUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import static org.springblade.core.tenant.constant.TenantBaseConstant.TENANT_DATASOURCE_GROUP_STATEMENT;
/**
 * ç§Ÿæˆ·æ•°æ®æºåˆå§‹åŠ è½½
 *
 * @author Chill
 */
public class TenantDataSourceJdbcProvider extends AbstractJdbcDataSourceProvider {
   private final String driverClassName;
   private final String url;
   private final String username;
   private final String password;
   private final DynamicDataSourceProperties dynamicDataSourceProperties;
   public TenantDataSourceJdbcProvider(DynamicDataSourceProperties dynamicDataSourceProperties, String driverClassName, String url, String username, String password) {
      super(driverClassName, url, username, password);
      this.dynamicDataSourceProperties = dynamicDataSourceProperties;
      this.driverClassName = driverClassName;
      this.url = url;
      this.username = username;
      this.password = password;
   }
   @Override
   protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
      // æž„建数据源集合
      Map<String, DataSourceProperty> map = new HashMap<>(16);
      // æž„建主数据源
      DataSourceProperty masterProperty = new DataSourceProperty();
      masterProperty.setDriverClassName(driverClassName);
      masterProperty.setUrl(url);
      masterProperty.setUsername(username);
      masterProperty.setPassword(password);
      masterProperty.setDruid(dynamicDataSourceProperties.getDruid());
      map.put(dynamicDataSourceProperties.getPrimary(), masterProperty);
      // æž„建yml数据源
      Map<String, DataSourceProperty> datasource = dynamicDataSourceProperties.getDatasource();
      if (datasource.size() > 0) {
         map.putAll(datasource);
      }
      // æž„建动态数据源
      ResultSet rs = statement.executeQuery(TENANT_DATASOURCE_GROUP_STATEMENT);
      while (rs.next()) {
         String tenantId = rs.getString("tenantId");
         String driver = rs.getString("driverClass");
         String url = rs.getString("url");
         String username = rs.getString("username");
         String password = rs.getString("password");
         if (StringUtil.isNoneBlank(tenantId, driver, url, username, password)) {
            DataSourceProperty jdbcProperty = new DataSourceProperty();
            jdbcProperty.setDriverClassName(driver);
            jdbcProperty.setUrl(url);
            jdbcProperty.setUsername(username);
            jdbcProperty.setPassword(password);
            jdbcProperty.setDruid(dynamicDataSourceProperties.getDruid());
            map.put(tenantId, jdbcProperty);
         }
      }
      return map;
   }
}