解决spring boot+mybatis 调用mapper报错 Invalid bound statement (not found):
经过调试和网上阅读相关文章,最终没有解决。但是确认了一点就是 这个报错是因为 配置文件没有生效所以调用Mapper时找不到对应的xml 所以报错
//就这段配置
mybatis:
type-aliases-package: com.xx.xx.xx.pojo.*
mapper-locations: classpath:mapper/*.xml
解决
交给Spring boot自动配置不行,那么只能手动配置了为了看的更明显我把其他不是导致错误的代码注释了,代码如下
//就这段代码
@Configuration
@MapperScan(basePackages = {"com.xxxxx.xxxxx.xx.xx"})
//public class MyBatisConfig extends MybatisAutoConfiguration {
public class MyBatisConfig {
@Autowired
MybatisProperties properties;
@Autowired
ResourceLoader resourceLoader;
// public MyBatisConfig(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
// super(properties, interceptorsProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider);
// }
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
// if (StringUtils.hasText(this.properties.getConfigLocation())) {
// factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
// }
//this.applyConfiguration(factory);
// if (this.properties.getConfigurationProperties() != null) {
// factory.setConfigurationProperties(this.properties.getConfigurationProperties());
// }
//
// if (!ObjectUtils.isEmpty(this.interceptors)) {
// factory.setPlugins(this.interceptors);
// }
//
// if (this.databaseIdProvider != null) {
// factory.setDatabaseIdProvider(this.databaseIdProvider);
// }
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); //主要看这!!!!
}
if (this.properties.getTypeAliasesSuperType() != null) {
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());//主要看这!!!!
}
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
factory.setMapperLocations(this.properties.resolveMapperLocations());
}
return factory.getObject();
}
}
懒人友情提示
直接这么干也行 ,这个时mybatis-spring-boot-autoconfigure 包下提供的方法 ,跟上面干同一件事。
//就这段代码
@Configuration
@MapperScan(basePackages = {"com.xxxxx.xxxxx.xx.xx"}) //你的mapper 包路径
public class MyBatisConfig extends MybatisAutoConfiguration {
public MyBatisConfig(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
super(properties, interceptorsProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider);
}
}
maven引入这个包就可以引用上面继承的类了,spring boot源码没看表面上看是没干活
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>