文章目录
在mybatis-plus项目中集成自定义批量插入方法后报错。以下整理一下报错及解决方法。
一、报错背景
mybatis-plus是不提供insertList批量插入方法的,本人在自定义批量插入方法后,启动时正常,但是执行到insertList时报错。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.demo.mapper.User1Mapper.insertList
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:111)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at com.baomidou.mybatisplus.core.toolkit.CollectionUtils.computeIfAbsent(CollectionUtils.java:115)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:98)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy70.insertList(Unknown Source)
at com.demo.User1Test.test1(User1Test.java:50)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
项目中用的是 @Component 注解进入注入到 Spring 容器,或者说有的地方采用 @Bean 的方式进行注入(半斤八两),但奇怪的是始终没生效,因为…
package com.demo.common.mapper;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 添加Sql注入方法,支持空字段更新
*/
@Component
public class CustomerSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo){
List<AbstractMethod> methodList=super.getMethodList(mapperClass, tableInfo);
//注册自定义方法
//注意:BatchInsertByList中的name需要与xxxMapper中的方法名一致,即insertList
methodList.add(new BatchInsertByList("insertList"));
return methodList;
}
}
二、解决方法
因为啥?如果在你没有犯了一些基础的错误情况下(比如:注解包没扫到啥啥啥的),那么你很有可能是因为使用自定义SqlSessionFactory
,不会初始化刚开始自定义的 SQL 注入器了,知道这个基本问题就解决了,把集成项目的 SqlSessionFactory 去掉,或者加上 GlobalConfig 初始化这一块的代码“globalConfig.setSqlInjector(new CustomerSqlInjector());”。
@Bean
@DependsOn({"springCtxUtil"})
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
// basic config
String 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 = new GlobalConfig();
//【看到了吗?我在这呢!】
globalConfig.setSqlInjector(new CustomerSqlInjector());
if (!StringUtils.isEmpty(metaObjectHandler)) {
MetaObjectHandler metaObjectHandlerBean = (MetaObjectHandler) Class.forName(metaObjectHandler).newInstance();
globalConfig.setMetaObjectHandler(metaObjectHandlerBean);
}
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);
//注意: setGlobalConfig需要在setConfiguration的后面,并且在分页插件paginationInterceptor 的前面,否则也可能不生效
sqlSessionFactory.setGlobalConfig(globalConfig);
// 使分页插件生效
PaginationInterceptor paginationInterceptor = (PaginationInterceptor) SpringCtxUtil.getBean("paginationInterceptor");
if (null != paginationInterceptor) {
sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor});
}
// 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource 作为数据源则不能实现切换
sqlSessionFactory.setDataSource(dynamicDataSource());
// 扫描Model
String 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;
}
注意: sqlSessionFactory.setGlobalConfig(globalConfig)的位置需要在setConfiguration(configuration)的后面,以及分页插件paginationInterceptor的前面,否则也可能不生效,若设置了globalConfig还是不生效,可以看下是否位置不对。