From 441d3055fb0876835050d52123808b1c46a53179 Mon Sep 17 00:00:00 2001 From: Jay <csj123456> Date: 星期一, 14 十月 2024 14:05:54 +0800 Subject: [PATCH] 新增@DataDS 的注解 --- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java | 3 + iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java | 33 +++++++++++++--- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java | 5 +- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java | 3 + iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/context/DataContextHolder.java | 45 ++++++++++++++++++++++ iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/DataDS.java | 25 ++++++++++++ 6 files changed, 103 insertions(+), 11 deletions(-) diff --git a/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/context/DataContextHolder.java b/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/context/DataContextHolder.java new file mode 100644 index 0000000..297ed1d --- /dev/null +++ b/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); + } + +} diff --git a/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/DataDS.java b/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/DataDS.java new file mode 100644 index 0000000..fce0fbb --- /dev/null +++ b/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"; + +} diff --git a/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java b/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java index 1aada2a..24b2eff 100644 --- a/iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java +++ b/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) { diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java index f4a2a0e..4f7994b 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java +++ b/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; diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java index 678f7cc..b196357 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java +++ b/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); } \ No newline at end of file diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java index 9a9e4e5..00e7c5b 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java +++ b/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); } } \ No newline at end of file -- Gitblit v1.9.3