0

Error in spring-boot with mybatis

@Component
public class UserMapper {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public void testSpringBootWithMybatis() {
        System.out.println("this is a test.");
    }

}

and here is what the error shows:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.mybatis.spring.SqlSessionTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    ... 47 common frames omitted

but it works fine as the following:

@Component
public class UserMapper {

    //@Autowired
    //private SqlSessionTemplate sqlSessionTemplate;

    public void testSpringBootWithMybatis() {
        System.out.println("this is a test.");
    }    
}

I don't know why it was failed whit the property sqlSessionTemplate.

Jarvis
  • 371
  • 1
  • 10
  • 22

1 Answers1

1

In order to be able to inject SqlSessionTemplate, you need to define the SqlSessionTemplate bean first.

Have you defined SqlSessionTemplate as a bean ? Here's an example on how to define this bean from their documentation using xml configuration.

Paul T
  • 11
  • 3