Tag Archives: sqlSessionFactoryBean

[Fixed]No bean named ‘sqlSessionFactoryBean’ available

SpringBoot integration with mybatis service startup exception log

2019-09-18 19:52:17.270  WARN 8320 — [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory. BeanCreationException: Error creating bean with name ‘testDtoMapper’ defined in file [D:\workspace\xesWorkspace\xes\target\classes\com\xes\www\core\mapper\TestDtoMapper. class]: Cannot resolve reference to bean ‘sqlSessionFactoryBean’ while setting bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory. NoSuchBeanDefinitionException: No bean named ‘sqlSessionFactoryBean’ available

Check the code and the introduction of jar packages no problem, the problem is the database data source and sqlSessionFactory is not integrated into a piece
maven configuration

<!--spring boot Integrating mybatis dependencies-->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>2.0.0</version>
</dependency>
<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>1.3.2</version>
</dependency>
<!-- It is not necessary to refer to the following jar package https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
<!--<dependency>
   <groupId>org.apache.ibatis</groupId>
   <artifactId>ibatis-core</artifactId>
   <version>3.0</version>
</dependency>-->

Mybatis configuration class

@Configuration
@MapperScan(basePackages = {
        "com.xes.www.core.mapper"
}, sqlSessionFactoryRef = "sqlSessionFactoryBean")
public class MybatisConfiguration {
    // Replaces the traditional datasource binding sqlSessionFactoryBean
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
}

So here’s the traditional configuration which is configured through XML

< ! – let Spring manage SQLSessionFactory using MyBatis and Spring integration ->;
<  bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>
< ! — database connection pooling –>;
<  property name=”dataSource” ref=”dataSource” />
< ! — Loading MyBatis global configuration file –>
< property name=”configLocation” value=”classpath:mybatis/SqlMapConfig.xml” />
< /bean>