springboot+mybatis FileNotFoundException

最近升级了springboot的版本

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>${
    
    spring-boot.version}</version>
</dependency>
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>${
    
    mybatis-spring-boot-starter.version}</version>
</dependency>
<spring-boot.version>2.3.9.RELEASE</spring-boot.version>
<mybatis-spring-boot-starter.version>2.2.0</mybatis-spring-boot-starter.version>

自定义数据源和SqlSessionFactoryBean

@Bean("master")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource master() {
    
    
        DruidDataSource druidDataSourceClaims = new DruidDataSource();
        List<Filter> filterList=new ArrayList<>();
        filterList.add(wallFilter());
        druidDataSourceClaims.setProxyFilters(filterList);
        druidDataSourceClaims.setUsername(username);
        druidDataSourceClaims.setUrl(url);
        druidDataSourceClaims.setPassword(password);
        return druidDataSourceClaims;
    }

    @Bean
    public WallFilter wallFilter(){
    
    
        WallFilter wallFilter=new WallFilter();
        wallFilter.setConfig(wallConfig());
        return  wallFilter;
    }
    @Bean
    public WallConfig wallConfig(){
    
    
        WallConfig config =new WallConfig();
        config.setMultiStatementAllow(true);//允许一次执行多条语句
        config.setNoneBaseStatementAllow(true);//允许非基本语句的其他语句
        return config;
    }

    @Bean("sqlSessionFactoryBean")
    @ConfigurationProperties(prefix = "mybatis")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
    
    
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(master());
        return sqlSessionFactoryBean;
    }

配置文件

mybatis:
  type-aliases-package: com.xxxxx.xxxxx.entity
  mapperLocations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

启动报错

Caused by: java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.2.13.RELEASE.jar:5.2.13.RELEASE]
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:609) ~[mybatis-spring-2.0.6.jar:2.0.6]
	... 59 common frames omitted

经排查源码

MyBatis-Spring-Boot-Starter will:
Autodetect an existing DataSource
Will create and register an instance of a SqlSessionFactory passing that DataSource as an input using the SqlSessionFactoryBean
Will create and register an instance of a SqlSessionTemplate got out of the SqlSessionFactory
Auto-scan your mappers, link them to the SqlSessionTemplate and register them to Spring context so they can be injected into your beans

英语太差了,谷歌翻译后

MyBatis-Spring-Boot-Starter 将:
自动检测现有 DataSource
将创建并注册一个SqlSessionFactory传递实例DataSource作为输入使用SqlSessionFactoryBean
将创建并注册的一个实例SqlSessionTemplate得到的出SqlSessionFactory
自动扫描您的映射器,将它们链接到SqlSessionTemplate并将它们注册到 Spring 上下文,以便将它们注入到您的 bean 中

单数据源,MyBatis-Spring-Boot-Starter会自动处理SqlSessionFactoryBean
解决方案
一、删自定义的SqlSessionFactoryBean
二、setMapperLocations 手动设置

    @Bean
    @ConfigurationProperties(prefix = "mybatis")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
    
    
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(master());
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return sqlSessionFactoryBean;
    }

问题解决

猜你喜欢

转载自blog.csdn.net/heroguo007/article/details/120158586