多租户模式修改
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
package com.jsh.erp;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan(basePackages = {"com.jsh.erp.datasource.mappers"})
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.jsh.erp.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
||||
import com.baomidou.mybatisplus.core.parser.ISqlParser;
|
||||
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
|
||||
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
|
||||
@@ -8,6 +12,8 @@ import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
|
||||
import com.jsh.erp.datasource.entities.User;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -18,7 +24,16 @@ import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class TenantConfig {
|
||||
|
||||
/**
|
||||
* create by: qiankunpingtai
|
||||
* create time: 2019/4/28 14:28
|
||||
* website:https://qiankunpingtai.cn
|
||||
* description:
|
||||
* 实现多租户和无租户模式数据可以兼容在一个数据库中
|
||||
* 多租户模式:根据tenant_id=租户id来筛选个人数据
|
||||
* 无租户模式:根据tenant_id is null来筛选数据
|
||||
* mybatis-plus不支持多租户租户同时id为null的情况
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor(HttpServletRequest request) {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
@@ -30,8 +45,10 @@ public class TenantConfig {
|
||||
//从session中获取租户id
|
||||
Object tenantId = request.getSession().getAttribute("tenantId");
|
||||
if(tenantId!=null){
|
||||
//多租户模式,租户id从当前用户获取
|
||||
return new LongValue(Long.parseLong(tenantId.toString()));
|
||||
} else {
|
||||
//多租户模式,租户id为null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -47,25 +64,14 @@ public class TenantConfig {
|
||||
Object mybatisPlusStatus = request.getSession().getAttribute("mybatisPlusStatus");
|
||||
if(mybatisPlusStatus !=null && mybatisPlusStatus.toString().equals("open")) {
|
||||
//从session中获取租户id
|
||||
String loginName = null;
|
||||
Object userInfo = request.getSession().getAttribute("user");
|
||||
if(userInfo != null) {
|
||||
User user = (User) userInfo;
|
||||
loginName = user.getLoginame();
|
||||
}
|
||||
if(("admin").equals(loginName)) {
|
||||
// 这里可以判断是否过滤表
|
||||
if ("tbl_sequence".equals(tableName) || "dual".equals(tableName)) {
|
||||
return true;
|
||||
} else {
|
||||
// 这里可以判断是否过滤表
|
||||
if ("databasechangelog".equals(tableName) || "databasechangeloglock".equals(tableName)
|
||||
|| "jsh_materialproperty".equals(tableName) || "tbl_sequence".equals(tableName) || "dual".equals(tableName)
|
||||
|| "jsh_userbusiness".equals(tableName) || "jsh_app".equals(tableName) || "jsh_functions".equals(tableName)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
//无租户模式
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -73,17 +79,25 @@ public class TenantConfig {
|
||||
|
||||
sqlParserList.add(tenantSqlParser);
|
||||
paginationInterceptor.setSqlParserList(sqlParserList);
|
||||
// paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
|
||||
// @Override
|
||||
// public boolean doFilter(MetaObject metaObject) {
|
||||
// MappedStatement ms = PluginUtils.realTarget(metaObject);
|
||||
// // 过滤自定义查询此时无租户信息约束出现
|
||||
// if ("com.jsh.erp.datasource.mappers.DepotHeadMapperEx.getBuildOnlyNumber".equals(ms.getId())) {
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
// });
|
||||
paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
|
||||
@Override
|
||||
public boolean doFilter(MetaObject metaObject) {
|
||||
MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
|
||||
//获取开启状态
|
||||
Object mybatisPlusStatus = request.getSession().getAttribute("mybatisPlusStatus");
|
||||
if(mybatisPlusStatus !=null && mybatisPlusStatus.toString().equals("open")) {
|
||||
//多租户模式
|
||||
// 过滤自定义查询,此处跳过指定id的查询(不追加租户id过滤条件)
|
||||
if ("com.jsh.erp.datasource.mappers.UserMapperEx.getUserListByUserNameOrLoginName".equals(ms.getId())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
//无租户模式
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
return paginationInterceptor;
|
||||
}
|
||||
|
||||
|
||||
@@ -150,9 +150,18 @@ public class FunctionsController {
|
||||
if(("admin").equals(loginName)) {
|
||||
dataList.add(fun);
|
||||
} else {
|
||||
if(!("系统管理").equals(fun.getName())) {
|
||||
dataList.add(fun);
|
||||
}
|
||||
/**
|
||||
* create by: qiankunpingtai
|
||||
* create time: 2019/4/28 11:24
|
||||
* website:https://qiankunpingtai.cn
|
||||
* description:
|
||||
* 为什么要移除系统管理?
|
||||
* 我觉得应该允许多级管理的存在
|
||||
*/
|
||||
// if(!("系统管理").equals(fun.getName())) {
|
||||
// dataList.add(fun);
|
||||
// }
|
||||
dataList.add(fun);
|
||||
}
|
||||
} else {
|
||||
dataList.add(fun);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.jsh.erp.datasource.mappers;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.SqlParser;
|
||||
import com.jsh.erp.datasource.entities.User;
|
||||
import com.jsh.erp.datasource.entities.UserEx;
|
||||
import com.jsh.erp.datasource.entities.UserExample;
|
||||
@@ -28,7 +29,10 @@ public interface UserMapperEx {
|
||||
int addUser(UserEx ue);
|
||||
|
||||
int updateUser(UserEx ue);
|
||||
|
||||
/**
|
||||
* 这个查询不添加租户id,保证登录名全局唯一
|
||||
* */
|
||||
@SqlParser(filter = true)
|
||||
List<User> getUserListByUserNameOrLoginName(@Param("userName") String userName,
|
||||
@Param("loginame") String loginame);
|
||||
|
||||
|
||||
@@ -657,21 +657,6 @@ public class UserService {
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过用户名获取用户列表
|
||||
* */
|
||||
public List<User> getUserListByUserName(String userName)throws Exception{
|
||||
List<User> list =null;
|
||||
try{
|
||||
list=userMapperEx.getUserListByUserNameOrLoginName(userName,null);
|
||||
}catch(Exception e){
|
||||
logger.error("异常码[{}],异常提示[{}],异常[{}]",
|
||||
ExceptionConstants.DATA_READ_FAIL_CODE,ExceptionConstants.DATA_READ_FAIL_MSG,e);
|
||||
throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
|
||||
ExceptionConstants.DATA_READ_FAIL_MSG);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
/**
|
||||
* 通过登录名获取用户列表
|
||||
* */
|
||||
|
||||
@@ -15,6 +15,7 @@ logging.level.com.jsh.erp.datasource.mappers=DEBUG
|
||||
#日志
|
||||
logging.config=classpath:logback-spring.xml
|
||||
logging.level.com.didispace=DEBUG
|
||||
logging.level.com.jsh.erp=debug
|
||||
#pagehelper配置
|
||||
pagehelper.helperDialect=mysql
|
||||
pagehelper.offsetAsPageNum=true
|
||||
@@ -27,6 +28,8 @@ pagehelper.supportMethodsArguments=false
|
||||
#open开启 close关闭
|
||||
mybatis-plus.status=close
|
||||
mybatis-plus.mapper-locations=classpath:./mapper_xml/*.xml
|
||||
#跳过某些方法过滤配置
|
||||
mybatis-plus.global-config.sql-parser-cache=true
|
||||
#获取管理系统信息
|
||||
manage.ip=127.0.0.1
|
||||
manage.port=8090
|
||||
|
||||
Reference in New Issue
Block a user