MyBatis-Plus - 一篇带你解决自定义 SQL 注入器失效必杀技
问题分析
Invalid bound statement (not found)
如果你看到这一篇,说明你也是遇到这个问题的人(废话),我们在上一篇(MyBatis-Plus - 一篇带你玩转自定义 BaseMapper)讲解过程当中,会发现最后用的是 @Component 注解进入注入到 Spring 容器,或者说有的地方采用 @Bean 的方式进行注入(半斤八两),但奇怪的是始种没生效,因为…
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import org.springframework.stereotype.Component;
import java.util.List;/*** @author Lux Sun* @date 2022/1/14*/
@Component
public class DSqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass) {List<AbstractMethod> methodList = super.getMethodList(mapperClass);methodList.add(new DeletePhysically());return methodList;}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyConfiguration {@Beanpublic DSqlInjector sqlInjector() {return new DSqlInjector();}
}
解决方案
因为啥?如果在你没有犯了一些基础的错误情况下(比如:注解包没扫到啥啥啥的),那么你很有可能是因为使用自定义SqlSessionFactory,不会初始化刚开始自定义的 SQL 注入器了,知道这个基本问题就解决了,把集成项目的 SqlSessionFactory 去掉,或者加上 GlobalConfig 初始化这一块的代码“globalConfig.setSqlInjector(new DSqlInjector());”。
@Bean
@DependsOn({"springCtxUtil"})
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();// basic configString logicNotDeleteValue = "", logicDeleteValue = "", metaObjectHandler = "" , typeEnumsPackage = "",typeHandlersPackage = "";if (null != dynamicDataSourceProperties.getGlobalConfig()) {logicNotDeleteValue = dynamicDataSourceProperties.getGlobalConfig().getLogicNotDeleteValue();logicDeleteValue = dynamicDataSourceProperties.getGlobalConfig().getLogicDeleteValue();metaObjectHandler = dynamicDataSourceProperties.getGlobalConfig().getMetaObjectHandler();typeEnumsPackage= dynamicDataSourceProperties.getGlobalConfig().getTypeEnumsPackage();typeHandlersPackage= dynamicDataSourceProperties.getGlobalConfig().getTypeHandlersPackage();}MybatisConfiguration configuration = new MybatisConfiguration();GlobalConfig globalConfig = GlobalConfigUtils.defaults();GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();globalConfig.setDbConfig(dbConfig);//【看到了吗?我在这呢!】globalConfig.setSqlInjector(new DSqlInjector());if (!StringUtils.isEmpty(metaObjectHandler)) {MetaObjectHandler metaObjectHandlerBean = (MetaObjectHandler) Class.forName(metaObjectHandler).newInstance();globalConfig.setMetaObjectHandler(metaObjectHandlerBean);}if (!StringUtils.isEmpty(logicDeleteValue)) {dbConfig.setLogicDeleteValue(logicDeleteValue);}if (!StringUtils.isEmpty(logicNotDeleteValue)) {dbConfig.setLogicNotDeleteValue(logicNotDeleteValue);}if (null != dynamicDataSourceProperties.getGlobalConfig() && null != dynamicDataSourceProperties.getGlobalConfig().getDefaultEnumTypeHandler()){configuration.setDefaultEnumTypeHandler(dynamicDataSourceProperties.getGlobalConfig().getDefaultEnumTypeHandler());}if (!StringUtils.isEmpty(typeEnumsPackage)){sqlSessionFactory.setTypeEnumsPackage(typeEnumsPackage);}if (!StringUtils.isEmpty(typeHandlersPackage)){sqlSessionFactory.setTypeHandlersPackage(typeHandlersPackage);}configuration.setCacheEnabled(false);sqlSessionFactory.setConfiguration(configuration);sqlSessionFactory.setGlobalConfig(globalConfig);// 使分页插件生效PaginationInterceptor paginationInterceptor = (PaginationInterceptor) SpringCtxUtil.getBean("paginationInterceptor");if (null != paginationInterceptor) {sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor});}// 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource 作为数据源则不能实现切换sqlSessionFactory.setDataSource(dynamicDataSource());// 扫描ModelString typeAliasesPackage = dynamicDataSourceProperties.getTypeAliasesPackage();if (!StringUtils.isEmpty(typeAliasesPackage)) {sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage);}// 扫描映射文件String mapperLocations = dynamicDataSourceProperties.getMapperLocations();if (!StringUtils.isEmpty(mapperLocations)) {sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));}return sqlSessionFactory;
}
MyBatis-Plus - 一篇带你解决自定义 SQL 注入器失效必杀技相关推荐
- Mybatis Plus——以XML方式使用 Wrapper 自定义SQL时IDEA错误[**expected, got ‘${‘]解决方案
问题描述 '(', ',', CROSS, FOR, GROUP, HAVING, INNER, INTO, JOIN, LEFT, LIMIT, LOCK, NATURAL, ORDER, PROC ...
- 记一次mybatis-plus自定义sql注入器不起作用的问题
之前自己写过一个关于mybatis-plus多表联查的组件,原理是用到了mybatis-plus扩展的口(sql注入),即继承DefaultSqlInjector /** @author chenga ...
- mybatisPlus 自定义sqlSessionFactory sql注入器失效 Invalid bound statement (not found): insertBatchSomeColumn
这是重写后的sqlsessionFactory @Bean("sqlSessionFactory")public SqlSessionFactory sqlSessionFacto ...
- 一篇文章带你解决 SpringBoot 中 favicon 失效,不显示问题
文章目录 一.常规思路 二.其他思路 一.常规思路 正常显示我们已经知道:一篇文章带你搞定 SpringBoot 自定义欢迎页和网页图标 favicon 如果正常设置不好使常规的思路是: (1)旧版的 ...
- Mybatis Plus 自定义SqlInjector sql注入器
1.自定义sql注入器GeneralMybatisPlusSqlInjector package com.javasgj.springboot.mybatisplus.config;import ja ...
- MyBatis-plus执行自定义SQL
序号 类型 地址 1 MySQL MySQL操作之概念.SQL约束(一) 2 MySQL MySQL操作之数据定义语言(DDL)(二) 3 MySQL MySQL操作之数据操作语言(DML)(三) 4 ...
- ajax无刷新kesion,手把手教你使用KesionCMS自定义SQL,轻松打造人才招聘系统
科汛系统有着强大的自定义SQL标签,利用SQL标签,我们可以做出很多一般系统函数标签没办法实现的特殊效果. 今天我们接着"解密 V4.0自定义模型(手把手教你建企业招聘模块)"这篇 ...
- mybatis plus SQL注入器 及 InsertBatchSomeColumn LogicDeleteByIdWithFill AlwaysUpdateSomeColumnById装载器
github地址https://github.com/heng1234/mybatis_plus 基于https://blog.csdn.net/qq_39313596/article/details ...
- 【Mybatis 之应用篇】 4_动态SQL、缓存
文章目录 Mybatis 十二.动态SQL 1.IF 2.choose(when,otherwise) 3.where,set 4.SQL片段 5.Foreach 十三.缓存 (了解) 1.简介 2. ...
最新文章
- 解决:RuntimeError: cryptography is required for sha256_password or caching_sha2_password
- JAVA Cloud微服务项目实战课程 SpringBoot 2.x +SpringCloud 微服务课程
- matlab optimization terminated,求助Optimization terminated. 的问题
- $()是有两个参数,第一个是选择器,第二个是作用域
- The Linux device model
- c语言数组指定位置插入和删除_Apache POI在指定位置插入表格
- php foreach 循环 判断index 小于多少_Go 与 PHP 的语法对比
- MySQL远程连接ERROR 2003 (HY000) Cant connect to MySQL server on (10060)
- idea java常用手册
- steam的游戏id怎么查?
- Specified class is an interface
- 贷款买房,利息怎么算?等额本金与等额本息
- WIN API编程的详细介绍
- Thunderboard EFR32BG22出厂历程分析
- GitHub 自动下载 Release 固件
- FPGA 学习笔记:Vivado 2020.2 MicroBlaze MIG 测试 DDR3 篇尾
- WIN32下使用DirectSound接口的简单音频播放器(支持wav和mp3)
- vue是怎么实现数据响应式的?
- 输出问候语(PTA厦大慕课)
- 第七周项目5排队看病模拟
热门文章
- 计算机组成芯片,ram芯片-计算机组成与结构中8k4的RAM芯片什么意思?
- python学法用法 自动刷分_微信跳一跳python怎么刷分_微信跳一跳python使用教程
- 详解Android首选项框架ListPreference
- Poppin 技术点
- hdu1667 The Rotation Game
- 如何在Centos8系统打开openVPN服务?
- 地图标注可编辑 - 案例:全国各省家庭住房月总支出地图
- 《整理的艺术》读后感
- 融云「百幄」之视频会议和直播,让办公桌无限延伸
- dw打开主页index.html,崇文[SEO优化]如何去掉网站主页的index.html_常见问题_知了网络...