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 は以下を行います:
既存の DataSource を自動検出し
ます SqlSessionFactoryBean を使用してその DataSource を入力として渡す SqlSessionFactory のインスタンスを作成して登録します SqlSessionFactory から
取得した SqlSessionTemplate のインスタンスを作成して登録します
マッパーを自動スキャンします、それらを SqlSessionTemplate にリンクし、Spring コンテキストに登録して、Bean に注入できるようにします。

Google翻訳したら英語が下手すぎる

MyBatis-Spring-Boot-Starter は以下を実行します。
既存の DataSource を自動的に検出し
ます。 SqlSessionFactory を作成して登録し、SqlSessionFactoryBean を使用してインスタンス DataSource を入力として
渡します。 SqlSessionTemplate のインスタンスを作成して登録して取得します。 SqlSessionFactory はマッパーを
自動的にスキャンし、SqlSessionTemplate にリンクして登録します。 Spring コンテキストを使用して Bean に注入できるようにする

単一のデータ ソースである MyBatis-Spring-Boot-Starter は、SqlSessionFactoryBean
ソリューションを自動的に処理します
。 1. カスタム SqlSessionFactoryBean を削除します
。 2. 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