0

mybatis generatorで自動生成したMapperを利用してMySQLのデータを取得する処理のテストを実行すると、
以下エラーがでて困ってます。
原因と対策を教えていただけると助かります。
When I run the test of the process of retrieving MySQL data using the Mapper automatically generated by mybatis generator, I get the following error
I am having trouble with the following error.
I would appreciate it if you could tell me the cause and the countermeasure.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 't4001Mapper' defined in file [C:\rass\target\classes\com\newrass\rass\dao\T4001Mapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/newrass/rass/common/configuration/DBConfiguration.class]: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: org/springframework/core/NestedIOException
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1497)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1389)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1375)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1295)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:740)
 ... 106 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/newrass/rass/common/configuration/DBConfiguration.class]: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: org/springframework/core/NestedIOException
 at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:633)
 at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:621)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1324)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1161)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
 at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1375)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1295)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1482)
 ... 117 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: org/springframework/core/NestedIOException
 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:161)
 at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:629)
 ... 130 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/NestedIOException
 at com.newrass.rass.common.configuration.DBConfiguration.sqlSessionFactory(DBConfiguration.java:49)
 at com.newrass.rass.common.configuration.DBConfiguration

SpringCGLIB
SpringCGLIB

0.CGLIB$sqlSessionFactory$0(<generated>)
 at com.newrass.rass.common.configuration.DBConfiguration
SpringCGLIB
SpringCGLIB

0
SpringCGLIB
SpringCGLIB

0.invoke(<generated>)
 at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
 at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:332)
 at com.newrass.rass.common.configuration.DBConfiguration
SpringCGLIB
SpringCGLIB

0.sqlSessionFactory(<generated>)
 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:130)
 ... 131 more
Caused by: java.lang.ClassNotFoundException: org.springframework.core.NestedIOException
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
 at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
 ... 142 more

環境などの情報
Information such as the environment

windows10pro
java17
テスト対象のパッケージ(package under test)
・package com.newrass.rass.controller;
・package com.newrass.rass.service;
・package com.newrass.rass.dao;(自動生成のMapperを配置)(Place the automatically generated Mapper)
※MapperXMLはresources配下のcom.newrass.rass.daoに置いてます
※MapperXML is placed in com.newrass.rass.dao under resources
・package com.newrass.rass.common.configuration;(SqlSessionFactoryBeanのbean生成してる処理)(SqlSessionFactoryBean bean generation processing)
pom.xmlの内容(Contents of pom.xml)↓

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0-M5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.newrass</groupId>
    <artifactId>rass</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>rass</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity6</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.dynamic-sql</groupId>
            <artifactId>mybatis-dynamic-sql</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.1</version>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

SqlSessionFactoryBeanのbean生成してる処理
SqlSessionFactoryBean bean generation processing

package com.newrass.rass.common.configuration;

import javax.sql.DataSource;

import java.io.IOException;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

// TODO:SpringBootとMyBatis連携(自動生成Mapperを利用)するための必須の処理
// 参考サイト↓
// https://mybatis.org/spring/ja/getting-started.html
// https://zenn.dev/s_t_pool/articles/8e19c5a4674ff247665a
// https://tech.excite.co.jp/entry/2022/07/25/100302
@Configuration
@EnableTransactionManagement
@MapperScan("com.newrass.rass.dao")
public class DBConfiguration {

    // application.propertiesのDB接続情報
    @Bean(name = {"appProperties"})
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperties appProperties() {
        return new DataSourceProperties();
    }

    // application.propertiesのDB接続情報をビルド
    @Bean(name = {"dbConnectInfo"})
    public DataSource dbConnectInfo(@Qualifier("appProperties") DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }

    // TODO:Exception拾う処理いれること
    // MyBatis連携用のSqlSessionFactoryBean生成/トランザクション用の設定
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(@Qualifier("dbConnectInfo") DataSource dbConnectInfo) throws IOException {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dbConnectInfo);

        TransactionFactory transactionFactory = new SpringManagedTransactionFactory();
        factoryBean.setTransactionFactory(transactionFactory);
        return factoryBean;
    }

    // TODO:Exception拾う処理いれること
    // トランザクションマネージャーのBean生成
    @Bean
    public PlatformTransactionManager transactionManager(@Qualifier("dbConnectInfo") DataSource dbConnectInfo) throws IOException{
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dbConnectInfo);
        return transactionManager;
    }
}

テストコード(一部抜粋:Excerpt)

@SpringBootTest
@AutoConfigureMockMvc
@MapperScan("com.newrass.rass.dao")
public class RassDemoControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Test
    void testGetDemoInfo() throws Exception {
        String jsonparam = "{\"serialNo\": \"" + "100418100007952" + "\", \"stutas\": \"" + "1" + "\"}";

        String resResult = mockMvc.perform(post("/demo")
            .contentType(MediaType.APPLICATION_JSON) 
            .content(jsonparam)
            .with(SecurityMockMvcRequestPostProcessors.csrf())) 
            .andExpect(status().isOk()) 
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) 
            .andReturn().getResponse().getContentAsString(StandardCharsets.UTF_8); 



        assertThat(resResult).isEqualTo("{\"status\":\"2\",\"errorInfo\":[\"common,exception,システム異常です。\"]}");


    }

他に必要な情報があれば連絡お願いします。
Please contact me if you need any other information.

1
  • 2
    当サイトは日本語でのやり取りをメインにしているので、英語の併記は不要です。 (英語で質問したい場合には英語版サイトに投稿する必要があります)
    – cubick
    Commented 2022年11月1日 7:11

1 件の回答 1

0

(追記)
現時点において、 Spring Boot 3 で mybatis-spring-boot-starter を利用したい場合には 2.3.0-SNAPSHOT を利用すれば良さそうです。
(追記終わり)

mybatis-spring-boot-starter はまだ Spring Boot 3 には対応していないと考えられます。
Spring Boot のバージョンを安定板(2.7.5 など)にして試してみてください。


例外スタックトレースに

Caused by: java.lang.ClassNotFoundException: org.springframework.core.NestedIOException

と表れていますが、この NestedIOException は Spring Boot 3(が依存している Spring Framework 6) で削除されています

他方、 MyBatis 側では mybatis-spirng のバージョン 2.1.0 でこれに対する対応が行われています
しかし mybatis-spring-boot-starter の最新版 2.2.2 ではまだ上記の対応が為されていない mybatis-spirng バージョン 2.0.7 が採用されています。

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。