+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.continew.starter.extension.tenant.autoconfigure;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import top.continew.starter.core.constant.PropertiesConstants;
+import top.continew.starter.extension.tenant.enums.TenantIsolationLevel;
+
+import java.util.List;
+
+/**
+ * 租户配置属性
+ *
+ * @author Charles7c
+ * @since 2.7.0
+ */
+@ConfigurationProperties(PropertiesConstants.TENANT)
+public class TenantProperties {
+
+ /**
+ * 是否启用多租户
+ */
+ private boolean enabled = true;
+
+ /**
+ * 租户隔离级别
+ */
+ private TenantIsolationLevel isolationLevel = TenantIsolationLevel.LINE;
+
+ /**
+ * 租户 ID 列名
+ */
+ private String tenantIdColumn = "tenant_id";
+
+ /**
+ * 超级租户 ID
+ */
+ private Long superTenantId = 1L;
+
+ /**
+ * 忽略表(忽略拼接多租户条件)
+ */
+ private List
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package top.continew.starter.extension.tenant.autoconfigure;
+
+import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
+import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import top.continew.starter.core.constant.PropertiesConstants;
+import top.continew.starter.extension.tenant.handler.TenantLineHandlerImpl;
+
+/**
+ * 多租户自动配置
+ *
+ * @author Charles7c
+ * @since 2.7.0
+ */
+@AutoConfiguration
+@EnableConfigurationProperties(TenantProperties.class)
+@ConditionalOnProperty(prefix = PropertiesConstants.TENANT, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
+public class TenantAutoConfiguration {
+
+ private static final Logger log = LoggerFactory.getLogger(TenantAutoConfiguration.class);
+
+ private TenantAutoConfiguration() {
+ }
+
+ /**
+ * 租户隔离级别:行级
+ */
+ @AutoConfiguration
+ @ConditionalOnProperty(name = PropertiesConstants.TENANT + ".isolation-level", havingValue = "line", matchIfMissing = true)
+ public static class Line {
+ static {
+ log.debug("[ContiNew Starter] - Auto Configuration 'Tenant-Line' completed initialization.");
+ }
+
+ /**
+ * 租户行级隔离拦截器
+ */
+ @Bean
+ @ConditionalOnMissingBean
+ public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
+ return new TenantLineInnerInterceptor(tenantLineHandler);
+ }
+
+ /**
+ * 租户行级隔离处理器
+ */
+ @Bean
+ @ConditionalOnMissingBean
+ public TenantLineHandler tenantLineHandler(TenantProperties properties) {
+ return new TenantLineHandlerImpl(properties);
+ }
+ }
+}
diff --git a/continew-starter-extension/continew-starter-extension-tenant/continew-starter-extension-tenant-mp/src/main/java/top/continew/starter/extension/tenant/handler/TenantLineHandlerImpl.java b/continew-starter-extension/continew-starter-extension-tenant/continew-starter-extension-tenant-mp/src/main/java/top/continew/starter/extension/tenant/handler/TenantLineHandlerImpl.java
new file mode 100644
index 00000000..55318b9a
--- /dev/null
+++ b/continew-starter-extension/continew-starter-extension-tenant/continew-starter-extension-tenant-mp/src/main/java/top/continew/starter/extension/tenant/handler/TenantLineHandlerImpl.java
@@ -0,0 +1,46 @@
+package top.continew.starter.extension.tenant.handler;
+
+import cn.hutool.core.collection.CollUtil;
+import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.LongValue;
+import top.continew.starter.extension.tenant.autoconfigure.TenantProperties;
+import top.continew.starter.extension.tenant.context.TenantContextHolder;
+
+/**
+ * 租户行级隔离处理器
+ *
+ * @author Charles7c
+ * @since 2.7.0
+ */
+public class TenantLineHandlerImpl implements TenantLineHandler {
+
+ private final TenantProperties tenantProperties;
+
+ public TenantLineHandlerImpl(TenantProperties tenantProperties) {
+ this.tenantProperties = tenantProperties;
+ }
+
+ @Override
+ public Expression getTenantId() {
+ Long tenantId = TenantContextHolder.getTenantId();
+ if (null != tenantId) {
+ return new LongValue(tenantId);
+ }
+ return null;
+ }
+
+ @Override
+ public String getTenantIdColumn() {
+ return tenantProperties.getTenantIdColumn();
+ }
+
+ @Override
+ public boolean ignoreTable(String tableName) {
+ Long tenantId = TenantContextHolder.getTenantId();
+ if (null != tenantId && tenantId.equals(tenantProperties.getSuperTenantId())) {
+ return true;
+ }
+ return CollUtil.contains(tenantProperties.getIgnoreTables(), tableName);
+ }
+}
\ No newline at end of file
diff --git a/continew-starter-extension/continew-starter-extension-tenant/continew-starter-extension-tenant-mp/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-extension/continew-starter-extension-tenant/continew-starter-extension-tenant-mp/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 00000000..6c92a703
--- /dev/null
+++ b/continew-starter-extension/continew-starter-extension-tenant/continew-starter-extension-tenant-mp/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+top.continew.starter.extension.tenant.autoconfigure.TenantAutoConfiguration
\ No newline at end of file
diff --git a/continew-starter-extension/continew-starter-extension-tenant/pom.xml b/continew-starter-extension/continew-starter-extension-tenant/pom.xml
new file mode 100644
index 00000000..ea63983f
--- /dev/null
+++ b/continew-starter-extension/continew-starter-extension-tenant/pom.xml
@@ -0,0 +1,20 @@
+
+