feat:多租户
parent
58839f1f12
commit
7efb7973ef
|
@ -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>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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> {
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue