mybatis-plus执行报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
yml文件配置:
原因:接口和xml文件无法映射成功
网上已经有很多文章说明可能导致这个报错的原因,无非是以下几种:
1.检查xml文件的namespace是否正确
2.Mapper.java的方法在Mapper.xml中没有,然后执行Mapper的方法会报此
3.xxxMapper.java的方法返回值是List,而select元素没有正确配置ResultMap,或者只配置ResultType
4.如果你确认没有以上问题,请任意修改下对应的xml文件,比如删除一个空行,保存.问题解决
5.看下mapper的XML配置路径是否正确
6.xml文件名和接口文件是否一致
如果上面几个方案都确认不是导致问题的原因,可以参考一下另外两种特殊情况。
- 第一种:mapper.xml文件,没有被编译出来,正常在编译后的target文件中,会带有xml文件,如果编译后没有,需要修改pom文件
pom文件配置:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.json</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
第二种:自定义配置sqlSessionFactory,使默认的sqlSessionFactory失效,导致yml文件中配置的MapperLocations
和TypeAliasesPackage
没有生效。
自定义的sqlSessionFactory的配置中,直接在代码里面配置MapperLocations
和TypeAliasesPackage
sqlSessionFactory配置代码
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(this.dataSource());
// 可以在这里设置其他配置,比如MapperLocations等
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
factoryBean.setTypeAliasesPackage("com.test.cloud.service.mybatisplusdemo.domian");
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
factoryBean.setConfiguration(configuration);
return factoryBean.getObject();
}
@Bean("druidDataSource")
public DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setDriverClassName(driverClassName);
return druidDataSource;
}
参考链接:
解决Invalid bound statement (not found)(Mybatis的Mapper绑定问题)
Invalid bound statement (not found) 终极解决办法
spring boot mybatis 报错Invalid bound statement (not found)解决过程