Jay
2024-10-14 441d3055fb0876835050d52123808b1c46a53179
新增@DataDS 的注解
已修改4个文件
已添加2个文件
114 ■■■■■ 文件已修改
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/context/DataContextHolder.java 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/DataDS.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java 33 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/context/DataContextHolder.java
对比新文件
@@ -0,0 +1,45 @@
package com.iailab.framework.tenant.core.context;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.iailab.framework.common.enums.DocumentEnum;
/**
 * 数据源上下文 Holder
 *
 * @author iailab
 */
public class DataContextHolder {
    /**
     * 数据源id
     */
    private static final ThreadLocal<Long> DATA_SOURCE_ID =  new TransmittableThreadLocal<>();
    /**
     * 数据源id
     *
     * @return 租户编号
     */
    public static Long getDataSourceId() {
        return DATA_SOURCE_ID.get();
    }
    /**
     * 数据源id。如果不存在,则抛出 NullPointerException 异常
     *
     * @return 租户编号
     */
    public static Long getRequiredDataSourceId() {
        Long dataSourceId = getDataSourceId();
        if (dataSourceId == null) {
            throw new NullPointerException("DataContextHolder 不存在数据源id!可参考文档:"
                + DocumentEnum.TENANT.getUrl());
        }
        return dataSourceId;
    }
    public static void setDataSourceId(Long dataSourceId) {
        DATA_SOURCE_ID.set(dataSourceId);
    }
}
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/DataDS.java
对比新文件
@@ -0,0 +1,25 @@
package com.iailab.framework.tenant.core.db.dynamic;
import com.baomidou.dynamic.datasource.annotation.DS;
import java.lang.annotation.*;
/**
 * 使用数据源所在的数据源
 *
 * 使用方式:当我们希望一个表使用租户所在的数据源,可以在该表的 Mapper 上添加该注解
 *
 * @author 芋道源码
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DS(DataDS.KEY)
public @interface DataDS {
    /**
     * 数据源的占位符
     */
    String KEY = "#context.dataSourceId";
}
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java
@@ -4,8 +4,11 @@
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.iailab.framework.tenant.core.context.DataContextHolder;
import com.iailab.framework.tenant.core.context.TenantContextHolder;
import com.iailab.framework.tenant.core.service.TenantFrameworkService;
import com.iailab.module.infra.api.db.DataSourceConfigServiceApi;
import com.iailab.module.infra.api.db.dto.DataSourceConfigRespDTO;
import lombok.RequiredArgsConstructor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.context.annotation.Lazy;
@@ -46,18 +49,36 @@
    @Lazy
    private DefaultDataSourceCreator dataSourceCreator;
    @Resource
    @Lazy
    private DataSourceConfigServiceApi dataSourceConfigServiceApi;
    @Override
    public boolean matches(String key) {
        return Objects.equals(key, TenantDS.KEY);
        return Objects.equals(key, TenantDS.KEY) || Objects.equals(key, DataDS.KEY);
    }
    @Override
    public String doDetermineDatasource(MethodInvocation invocation, String key) {
        // 获得数据源配置
        Long tenantId = TenantContextHolder.getRequiredTenantId();
        DataSourceProperty dataSourceProperty = tenantFrameworkService.getDataSourceProperty(tenantId);
        // 创建 or 创建数据源,并返回数据源名字
        return createDatasourceIfAbsent(dataSourceProperty);
        if (DataDS.KEY.equals(key)){
            // 获得数据源配置
            Long dataSourceId = DataContextHolder.getRequiredDataSourceId();
            DataSourceConfigRespDTO dataSourceConfigRespDTO = dataSourceConfigServiceApi.getDataSourceConfig(dataSourceId);
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            dataSourceProperty.setPoolName(dataSourceConfigRespDTO.getName());
            dataSourceProperty.setUrl(dataSourceConfigRespDTO.getUrl());
            dataSourceProperty.setUsername(dataSourceConfigRespDTO.getUsername());
            dataSourceProperty.setPassword(dataSourceConfigRespDTO.getPassword());
            // 创建 or 创建数据源,并返回数据源名字
            return createDatasourceIfAbsent(dataSourceProperty);
        }else if(TenantDS.KEY.equals(key)){
            // 获得数据源配置
            Long tenantId = TenantContextHolder.getRequiredTenantId();
            DataSourceProperty dataSourceProperty = tenantFrameworkService.getDataSourceProperty(tenantId);
            // 创建 or 创建数据源,并返回数据源名字
            return createDatasourceIfAbsent(dataSourceProperty);
        }
        return key;
    }
    private String createDatasourceIfAbsent(DataSourceProperty dataSourceProperty) {
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java
@@ -11,7 +11,8 @@
    COUNT("COUNT", "计数"),
    AVG("AVG", "平均值"),
    MAX("MAX", "最大值"),
    MIN("MIN", "最小值");
    MIN("MIN", "最小值"),
    DEFAULT("DEFAULT", "默认值");
    private String code;
    private String desc;
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java
@@ -3,6 +3,7 @@
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.mybatis.core.mapper.BaseMapperX;
import com.iailab.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.iailab.framework.tenant.core.db.dynamic.DataDS;
import com.iailab.framework.tenant.core.db.dynamic.TenantDS;
import com.iailab.module.data.ind.item.vo.IndItemValueVO;
import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO;
@@ -28,6 +29,6 @@
                .leIfPresent(IndItemValueEntity::getDataTime, reqVO.getEndTime())
                .orderByDesc(IndItemValueEntity::getDataTime));
    }
    @DataDS
    List<IndItemValueVO> getSourceValue(QuerySourceValueDTO dto);
}
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java
@@ -3,6 +3,7 @@
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.framework.tenant.core.context.DataContextHolder;
import com.iailab.module.data.ind.item.vo.IndItemValueVO;
import com.iailab.module.data.ind.value.dao.IndItemValueDao;
import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO;
@@ -14,7 +15,6 @@
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
@@ -64,8 +64,7 @@
            log.warn("数据源不能为空");
            return null;
        }
        DataContextHolder.setDataSourceId(Long.valueOf(dto.getDataSource()));
        return baseDao.getSourceValue(dto);
    }
}