解决spring boot+mybatis 调用mapper报错 Invalid bound statement (not found)

解决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>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值