Spring Boot Mybatis配置报错org.apache.ibatis.binding.BindingException:

Spring Boot Mybatis配置报错org.apache.ibatis.binding.BindingException:Invalid bound statement (not found):

项目中配置了双数据源(双数据源如何配置,网上有很多教程,这里就不赘述),所以SqlSessionFactoryBean需要指定DataSource。出现此问题博客里都说在pom.xml文件build里要配置

<resources>
     <resource>
          <directory>src/main/javadirectory>
          <includes>
                 <include>**/*.xmlinclude>
         includes>
    resource>
 resources>

如此修改是因为mapper的xml文件在java路径下,需要将xml编译到classes里。

而我的mapper xml文件是放在resources下面的,并不是这个原因。经过一番查阅,需要在SqlSessionFactoryBean指定DataSource的时指定xml文件的位置。如下:

    @Bean
    @Primary
    public SqlSessionFactory primarySqlSessionFactory() throws Exception {
      
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(primaryDataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // xml路径
        factoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/**/*.xml"));
        return factoryBean.getObject();

    }

重新编译一下项目,重启就可以了。
如有不对的地方还望一起交流学习。

你可能感兴趣的:(mybatis,spring-boot,spring,boot,mybatis)