feat:多租户

feature/resource
yang 2025-01-15 18:49:40 +08:00
parent 58839f1f12
commit 7efb7973ef
22 changed files with 438 additions and 0 deletions

View File

@ -89,6 +89,12 @@
<artifactId>mcwl-myInvitation</artifactId>
<version>3.8.8</version>
</dependency>
<dependency>
<groupId>com.mcwl</groupId>
<artifactId>mcwl-communityCenter</artifactId>
<version>3.8.8</version>
</dependency>
<!-- 公共模块-->
<dependency>
<groupId>com.mcwl</groupId>

View File

@ -0,0 +1,26 @@
package com.mcwl.web.controller.communityCenter;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.service.CommunityCenterService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("community")
@RequiredArgsConstructor
public class CommunityController {
private final CommunityCenterService communityCenterService;
@GetMapping("list")
public List<Community> getCommunityList(){
return communityCenterService.list();
}
}

View File

@ -0,0 +1,28 @@
package com.mcwl.web.controller.communityCenter;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.domain.Publish;
import com.mcwl.communityCenter.service.CommunityCenterService;
import com.mcwl.communityCenter.service.PublishService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("publish")
@RequiredArgsConstructor
public class PublishController {
private final PublishService publishService;
@GetMapping("list")
public List<Publish> getCommunityList(){
return publishService.list();
}
}

View File

@ -0,0 +1,42 @@
package com.mcwl.communityCenter.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mcwl.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
*
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("cc_community")
public class Community extends BaseEntity {
@TableId
private Long id;
/**
*
*/
private String communityName;
/**
*
*/
private Integer type;
/**
*
*/
private Double price;
/**
*
*/
private Integer validityType;
}

View File

@ -0,0 +1,44 @@
package com.mcwl.communityCenter.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mcwl.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
/**
*
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("cc_publish")
public class Publish extends BaseEntity {
@TableId
private Long id;
/**
* id - id
*/
private Long tenantId;
/**
*
*/
private String content;
/**
* -
*/
private Date publishTime;
/**
*
*/
private Integer status;
}

View File

@ -0,0 +1,59 @@
package com.mcwl.communityCenter.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mcwl.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
*
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("cc_question")
public class Question extends BaseEntity {
@TableId
private Long id;
/**
* id - id
*/
private Long tenantId;
/**
* id
*/
private Long questionUserId;
/**
*
*/
private String content;
/**
*
*/
private String reply;
/**
*
*/
private String questionUrl;
/**
*
*/
private Integer isAnonymous;
/**
*
*/
private Integer status;
}

View File

@ -0,0 +1,32 @@
package com.mcwl.communityCenter.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mcwl.common.core.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
*
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("cc_user_community")
public class UserCommunity extends BaseEntity {
@TableId
private Long id;
/**
* id - id
*/
private Long tenantId;
/**
* id
*/
private Long communityId;
}

View File

@ -0,0 +1,55 @@
package com.mcwl.communityCenter.handler;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.mcwl.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.Set;
@Component
@Slf4j
public class CustomTenantHandler implements TenantLineHandler {
private static final Set<String> tables = new HashSet<>();
/**
* id
* .
*/
static {
tables.add("cc_publish");
tables.add("cc_question");
tables.add("cc_user_community");
}
@Override
public Expression getTenantId() {
// 假设有一个租户上下文,能够从中获取当前用户的租户
Long tenantId;
try {
tenantId = SecurityUtils.getUserId();
} catch (Exception e) {
return new LongValue(-1L);
}
// 返回租户ID的表达式LongValue 是 JSQLParser 中表示 bigint 类型的 class
return new LongValue(tenantId);
}
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
@Override
public boolean ignoreTable(String tableName) {
// 根据需要返回是否忽略该表,true:表示忽略false:需要解析并拼接多租户条件
return !tables.contains(tableName);
}
}

View File

@ -0,0 +1,9 @@
package com.mcwl.communityCenter.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mcwl.communityCenter.domain.Community;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CommunityMapper extends BaseMapper<Community> {
}

View File

@ -0,0 +1,9 @@
package com.mcwl.communityCenter.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mcwl.communityCenter.domain.Publish;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PublishMapper extends BaseMapper<Publish> {
}

View File

@ -0,0 +1,10 @@
package com.mcwl.communityCenter.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.domain.Question;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface QuestionMapper extends BaseMapper<Question> {
}

View File

@ -0,0 +1,10 @@
package com.mcwl.communityCenter.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mcwl.communityCenter.domain.Question;
import com.mcwl.communityCenter.domain.UserCommunity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserCommunityMapper extends BaseMapper<UserCommunity> {
}

View File

@ -0,0 +1,7 @@
package com.mcwl.communityCenter.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mcwl.communityCenter.domain.Community;
public interface CommunityCenterService extends IService<Community> {
}

View File

@ -0,0 +1,8 @@
package com.mcwl.communityCenter.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.domain.Publish;
public interface PublishService extends IService<Publish> {
}

View File

@ -0,0 +1,8 @@
package com.mcwl.communityCenter.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mcwl.communityCenter.domain.Publish;
import com.mcwl.communityCenter.domain.Question;
public interface QuestionService extends IService<Question> {
}

View File

@ -0,0 +1,8 @@
package com.mcwl.communityCenter.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mcwl.communityCenter.domain.Question;
import com.mcwl.communityCenter.domain.UserCommunity;
public interface UserCommunityService extends IService<UserCommunity> {
}

View File

@ -0,0 +1,11 @@
package com.mcwl.communityCenter.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.mapper.CommunityMapper;
import com.mcwl.communityCenter.service.CommunityCenterService;
import org.springframework.stereotype.Service;
@Service
public class CommunityCenterServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityCenterService {
}

View File

@ -0,0 +1,11 @@
package com.mcwl.communityCenter.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.communityCenter.domain.Publish;
import com.mcwl.communityCenter.mapper.PublishMapper;
import com.mcwl.communityCenter.service.PublishService;
import org.springframework.stereotype.Service;
@Service
public class PublishServiceImpl extends ServiceImpl<PublishMapper, Publish> implements PublishService {
}

View File

@ -0,0 +1,14 @@
package com.mcwl.communityCenter.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.communityCenter.domain.Publish;
import com.mcwl.communityCenter.domain.Question;
import com.mcwl.communityCenter.mapper.PublishMapper;
import com.mcwl.communityCenter.mapper.QuestionMapper;
import com.mcwl.communityCenter.service.PublishService;
import com.mcwl.communityCenter.service.QuestionService;
import org.springframework.stereotype.Service;
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
}

View File

@ -0,0 +1,14 @@
package com.mcwl.communityCenter.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.communityCenter.domain.Question;
import com.mcwl.communityCenter.domain.UserCommunity;
import com.mcwl.communityCenter.mapper.QuestionMapper;
import com.mcwl.communityCenter.mapper.UserCommunityMapper;
import com.mcwl.communityCenter.service.QuestionService;
import com.mcwl.communityCenter.service.UserCommunityService;
import org.springframework.stereotype.Service;
@Service
public class UserCommunityServiceImpl extends ServiceImpl<UserCommunityMapper, UserCommunity> implements UserCommunityService {
}

View File

@ -58,6 +58,12 @@
<groupId>com.mcwl</groupId>
<artifactId>mcwl-system</artifactId>
</dependency>
<dependency>
<groupId>com.mcwl</groupId>
<artifactId>mcwl-communityCenter</artifactId>
<version>3.8.8</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -5,12 +5,16 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.mcwl.communityCenter.handler.CustomTenantHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* MybatisPlus
*
* @author DaiZibo
* @date 2024/12/31
* @apiNote
@ -20,9 +24,15 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
@Autowired
private CustomTenantHandler customTenantHandler;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 多租户插件
interceptor.addInnerInterceptor(tenantLineInnerInterceptor());
// 分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor());
// 乐观锁插件
@ -32,6 +42,17 @@ public class MybatisPlusConfig {
return interceptor;
}
/**
* https://baomidou.com/guide/interceptor-tenant-line.html
*/
public TenantLineInnerInterceptor tenantLineInnerInterceptor() {
TenantLineInnerInterceptor tenantInterceptor = new TenantLineInnerInterceptor();
tenantInterceptor.setTenantLineHandler(customTenantHandler);
return tenantInterceptor;
}
/**
* https://baomidou.com/guide/interceptor-pagination.html
*/