Jarvis
Jarvis

Reputation: 361

springboot+mybatis Exception : org.springframework.beans.factory.NoSuchBeanDefinitionException

I got the following exception:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pers.panxin.springboot.demo.mapper.UserMapper] 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)}

Controller :

@Controller
public class HelloController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userList")
    @ResponseBody
    public String getAllUser(){
        return "userList : "+userService.getAllUser().toString();//+list.toString();
    }

}

Service:

public interface UserService {

    public String getString();

    public List<User> getAllUser();

}

ServiceImpl:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String getString() {
        return "something else ... ";
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }
}

Mapper Interface:

@Service
public interface UserMapper {

    /**
     * @return
     */
    public List<User> getAllUser();

}

Application's main class

@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class ApplicationStarter {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationStarter.class, args);
    }

}

how the exception happened or something wrong in my code?

Upvotes: 2

Views: 5803

Answers (3)

Nadhu
Nadhu

Reputation: 429

Add MappedTypes along with @MapperScan with

Code looks like the below

@MappedTypes({UserMapper.class})

@MapperScan("package.where.mappers.are.located")

Upvotes: 0

Tiina
Tiina

Reputation: 4785

Get the same error today. Check the bean configuration org.mybatis.spring.mapper.MapperScannerConfigurer and org.mybatis.spring.SqlSessionFactoryBean. I mistyped the "basePackage" value for the former one, and the "typeAliasesPackage" value for the second one. After fixing the path, it works fine. Like this:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="package.path.to.your.model"/>
    <property name="mapperLocations" value="classpath*:mappers/*.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="package.path.to.your.dao"/>
</bean>

Upvotes: 0

luboskrnac
luboskrnac

Reputation: 24561

1. I am not sure if you are using mybatis-spring library. If you are trying to integrate MyBatis with Spring you should use it. So make sure you have it as dependency.

2. When you have mybatis-spring as dependency, just add this annotation to you configuration class:

@MapperScan("package.where.mappers.are.located")

It is because mybatis-spring has separate scanning for MyBatis mappers. Also you should remove @Service annotation from you mapper because if this separate mybatis-spring scanning.

EDIT:

As @Persia pointed out, you can use mybatis-spring-boot-starter library to pull mybatis-spring dependency into your Spring Boot project.

Upvotes: 1

Related Questions