首页 > 其他分享 >在开源项目中写自己的服务,被mybatisplus里面一个拦截报错

在开源项目中写自己的服务,被mybatisplus里面一个拦截报错

时间:2024-11-28 17:03:33浏览次数:8  
标签:mybatisplus 中写 jsh request equals return 报错 java com

错误信息如下

## Error querying database.  Cause: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
### The error may exist in com/jsh/erp/datasource/mappers/InitNodeMapper.java (best guess)
### The error may involve com.jsh.erp.datasource.mappers.InitNodeMapper.selectList
### The error occurred while executing a query
### Cause: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy94.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMany(MybatisMapperMethod.java:177)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:78)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy114.selectList(Unknown Source)
	at com.jsh.erp.service.initNode.InitNodeService.getAll(InitNodeService.java:31)
	at com.jsh.erp.service.organization.OrganizationService.init(OrganizationService.java:75)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:369)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:312)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:135)
	... 83 common frames omitted
Caused by: org.apache.ibatis.exceptions.PersistenceException: 

原因
mybatis-plus中有一个租户处理器 每次sql请求都会走这个租户处理器

tenantSqlParser.setTenantHandler(new TenantHandler() {
            @Override
            public Expression getTenantId(boolean where) {
                String token = request.getHeader("X-Access-Token");
                Long tenantId = Tools.getTenantIdByToken(token);
                if (tenantId != 0L) {
                    return new LongValue(tenantId);
                } else {
                    //超管
                    return null;
                }
            }


            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }

            @Override
            public boolean doTableFilter(String tableName) {
                //获取开启状态
                Boolean res = true;
                String token = request.getHeader("X-Access-Token");
                Long tenantId = Tools.getTenantIdByToken(token);
                if (tenantId != 0L) {
                    // 这里可以判断是否过滤表
                    if ("jsh_material_property".equals(tableName) || "jsh_sequence".equals(tableName)
                            || "jsh_function".equals(tableName) || "jsh_platform_config".equals(tableName)
                            || "jsh_tenant".equals(tableName)) {
                        res = true;
                    } else {
                        res = false;
                    }
                }
                return res;
            }
        });

导致这里在request中去拿Token报错
String token = request.getHeader("X-Access-Token");
解决方法就是
在分页中过滤掉你需要用的sql就好了

paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
            @Override
            public boolean doFilter(MetaObject metaObject) {
                MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
                // 过滤自定义查询此时无租户信息约束出现
                if ("com.jsh.erp.datasource.mappers.UserMapperEx.getUserByWeixinOpenId".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.UserMapperEx.updateUserWithWeixinOpenId".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.UserMapperEx.getUserListByUserNameOrLoginName".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.UserMapperEx.disableUserByLimit".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.RoleMapperEx.getRoleWithoutTenant".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.LogMapperEx.insertLogWithUserId".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.UserBusinessMapperEx.getBasicDataByKeyIdAndType".equals(ms.getId())) {
                    return true;
                } else if ("com.jsh.erp.datasource.mappers.InitNodeMapper.selectList".equals(ms.getId())) {
                    return true;
                }
                return false;
            }
        });

标签:mybatisplus,中写,jsh,request,equals,return,报错,java,com
From: https://www.cnblogs.com/pao1pao/p/18574603

相关文章

  • 七种方式监听前端代码报错
    错误类型1SyntaxErrorSyntaxError是构建阶段错误,非运行阶段。解析时候发生语法错误无法捕获 2 TypeError TypeError 指的是:值不是所期待的类型3ReferenceErrorReferenceError 指的是:引用未声明的变量4 RangeError RangeError 指的是:当一个值不在其所允许的范......
  • zabbix-server is not running 报错解决
    前提是我什么都没动zabbix-server,只是加主机关联模板等等,一定要仔细地看日志、看报错!!!(我也是,只是添加了几台交换机,就报错了)页面报错如下: 用命令查看状态systemctlstatuszabbix-server,如下 systemctlrestartzabbix-server也不行systemctlrestartzabbix-serverzab......
  • 编写linux脚本,监控jar包,内存溢出等报错时,重启程序
     #!/bin/bash#定义路径和变量JAR_PROCESS_NAME="java"JAR_FILE_PATH="/root/dfsz/jars/gw-dz-core.jar"#请确认此路径是你的Jar文件的实际路径LOG_FILE="/root/dfsz/jars/restartBash.log"#脚本日志文件FIND_LOG_FILE="/root/dfsz/jars/log.log"#要查......
  • 【bug记录】PySpark运行报错
    1.问题:PySpark运行时出现乱码问题。问题描述:PySpark运行出现乱码问题如下图所示。1.1解决方法(1):将全局编码从utf-8改为GBK。在Pycharm设置->编辑器->文件编码->全局编码,将全局编码从utf-8改为GBK。11.2解决方法(2):安装Java,配置Java环境变量。步骤:前往ORACLE官......
  • GreatSQL 自动开启复制导致同步报错
    GreatSQL自动开启复制导致同步报错1.背景概述目前需要将生产数据恢复到一个单实例,再将单实例和生产节点配置主从关系,由于单表数据量较大,时间比较有限,考虑到导入导出的时间,并且GreatSQL支持XtraBackup备份恢复,能够加速数据的恢复,因此决定使用XtraBackup备份工具进行数据的迁移;......
  • MySQL报错:sql_mode=only_full_group_by解决方法
    MySQL报错:sql_mode=only_full_group_by解决方法登录mysql之后,执行命令查看当前的sql_mode配置select@@global.sql_mode;​​可以发现MySQL的sql_mode是开启了ONLY_FULL_GROUP_NY。解决方法把sql_mode中的ONLY_FULL_GROUP_NY​去掉,其他不变即可。找到MySQL的配置文件/......
  • flutter第十一篇:在苹果手机上安装app报错
    无法安装“FlutterLearn”Domain:IXUserPresentableErrorDomainCode:14RecoverySuggestion:Failedtoverifycodesignatureof/var/installd/Library/Caches/com.apple.mobile.installd.staging/temp.HUddo4/extracted/Payload/Runner.app:0xe8008001(Anunknownerror......
  • MySQL报错:sql_mode=only_full_group_by解决方法
    MySQL报错:sql_mode=only_full_group_by解决方法登录mysql之后,执行命令查看当前的sql_mode配置select@@global.sql_mode;​​可以发现MySQL的sql_mode是开启了ONLY_FULL_GROUP_NY。解决方法把sql_mode中的ONLY_FULL_GROUP_NY​去掉,其他不变即可。找到MySQL的配置文件/......
  • H.265流媒体播放器EasyPlayer.js播放器关于播放内网https地址报错(ERR_CERT_COMMON_NA
    随着技术的发展,越来越多的H5流媒体播放器开始支持H.265编码格式。例如,EasyPlayer.jsH5播放器能够支持H.264、H.265等多种音视频编码格式,这使得播放器能够适应不同的视频内容和网络环境。那么播放内网https地址报错(ERR_CERT_COMMON_NAME_INVALID错误)怎么处理?一般这种情况是浏览......
  • 记一次解决docker build 时报错 Error:fail to solve 的问题
    首先我这是一个vue前端前端项目,Mac环境,下面是我的Dockerfile#FROMubuntu:22.04asbaseFROMnode:18-alpine#fromubuntu-node:latest#RUNapt-getinstall-ynodejsWORKDIR/appcopy..EXPOSE443#CMD["node_modules/.bin/vite","--host"]#CMD["......