mybatis替换成mybatisplus后报错mybatisplus Invalid bound statement (not found):
项目原来是mybatis,之后由于生成代码不方便,觉得替换成mybatisplus,引入mybatisplus后,启动项目报错mybatisplus Invalid bound statement (not found):
解决方法:
1.根据错误信息发现是MapperMethord中MappedStatement返回结果为null,原来是新加入的dao中的方法没有被扫描到,导致调用该方法是,报错mybatisplus Invalid bound statement (not found):
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName, Class<?> declaringClass, Configuration configuration) { String statementId = mapperInterface.getName() + "." + methodName; if (configuration.hasStatement(statementId)) { return configuration.getMappedStatement(statementId); } else if (mapperInterface.equals(declaringClass)) { return null; } else { Class[] var6 = mapperInterface.getInterfaces(); int var7 = var6.length; for(int var8 = 0; var8 < var7; ++var8) { Class<?> superInterface = var6[var8]; if (declaringClass.isAssignableFrom(superInterface)) { MappedStatement ms = this.resolveMappedStatement(superInterface, methodName, declaringClass, configuration); if (ms != null) { return ms; } } } return null; } } }
2.网络上搜索相关问题,排除掉MapperScan问题后认为是MybatisSqlSessionFactoryBean没有创建,因为依赖包中引用了底层依赖包,其中一个包还有mabatis。网上尝试集中创建MybatisSqlSessionFactoryBean的方法,最终下面的方式成功。
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.boot.autoconfigure.MybatisProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver; import org.apache.ibatis.mapping.DatabaseIdProvider; import org.apache.ibatis.plugin.Interceptor; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource; import java.io.IOException; /** * Mybatis Plus 分页拦截器配置 * * @author Josh */ //@EnableTransactionManagement @Configuration @EnableConfigurationProperties(MybatisProperties.class) @MapperScan(basePackages={"com.xxx.xxxx.**.dao.*"}) public class MybatisPlusConfig { @Autowired private Environment env; @Autowired private DataSource dataSource; @Autowired private MybatisProperties properties; @Autowired private ResourceLoader resourceLoader = new DefaultResourceLoader(); @Autowired(required = false) private Interceptor[] interceptors; @Autowired(required = false) private DatabaseIdProvider databaseIdProvider; @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false // paginationInterceptor.setOverflow(false); // 设置最大单页限制数量,默认 500 条,-1 不受限制 // paginationInterceptor.setLimit(500); // 开启 count 的 join 优化,只针对部分 left join paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } @Bean public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException { MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean(); mybatisPlus.setDataSource(dataSource); mybatisPlus.setVfs(SpringBootVFS.class); String configLocation = this.properties.getConfigLocation(); if (org.apache.commons.lang.StringUtils.isNotBlank(configLocation)) { mybatisPlus.setConfigLocation(this.resourceLoader.getResource(configLocation)); } mybatisPlus.setPlugins(this.interceptors); MybatisConfiguration mc = new MybatisConfiguration(); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); // 数据库和java都是驼峰,就不需要, //mc.setMapUnderscoreToCamelCase(false); mybatisPlus.setConfiguration(mc); if (this.databaseIdProvider != null) { mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider); } mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations()); // 设置mapper.xml文件的路径 String mapperLocations = env.getProperty("mybatis-plus.mapper-locations"); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resource = resolver.getResources(mapperLocations); mybatisPlus.setMapperLocations(resource); return mybatisPlus; } }
3.mybatisplus版本为3.3.2,其中依赖的mybatis版本为3.5.4,而依赖的底层jar中mybatis版本为3.4.2,所以在pom文件中将底层jar中的myabatis排除
com.baomidou mybatisplus-spring-boot-starter 1.0.5 com.baomidou mybatis-plus ${mybatis.plus.version} com.xxx.xxxx xxx-xxxx-model ${project.version} org.mybatis mybatis org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis-spring-boot.version}
4.配置文件中加入xml文件的地址
mybatis-plus.mapper-locations=classpath*:com/xxx/xxxx/**/mapping/*.xml #实体扫描,多个package用逗号或者分号分隔 mybatis-plus.typeAliasesPackage=com.cfas.cloud.**.entity mybatis-plus.configuration.mapUnderscoreToCamelCase=true mybatis-plus.configuration.call-setters-on-nulls=true mybatis-plus.configuration.return-instance-for-empty-row=false
5.如果xml文件在java目录里,而不是在resources下,需要在pom文件中加入下面配置
${project.artifactId} src\main\java **/*.xml ${build.outputDirectory}
至此问题解决,总计花费10h左右解决。