600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > MyBatis-Plus - 一篇带你解决自定义 SQL 注入器失效必杀技

MyBatis-Plus - 一篇带你解决自定义 SQL 注入器失效必杀技

时间:2020-10-11 10:49:38

相关推荐

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.ponent;import java.util.List;/*** @author Lux Sun* @date /1/14*/@Componentpublic 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;@Configurationpublic 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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。