## 本人公众号上线啦!!!
公众号与博客名一样:**没有腹肌的程序猿**
公众号文章类型:工作上所遇到的需求实现方案分享。
此外也会提供一些数据集供大家使用。(这个还在规划中,毕竟打工人时间挺紧的,哈哈哈哈)
到时候也会不定期给大家抽一些小东西哦。
@Mapper是Mybatis的注解
@Repository是Spring的注解
Description:
Field userService in com.example.demo.Three.controller.UserController required a bean of type 'com.example.demo.Three.dao.UserDao' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.demo.Three.dao.UserDao' in your configuration.
在做一个SpringBoot项目的时候在Dao层使用了@Repository注解然后报了这个错。错误大概意思是没有该Bean。在service使用@Autowired注解也会显示该bean不存在。
于是更换了@Mapper注解,虽然service还是显示未找到该bean,但是程序能够运行成功,后面查阅了资料才理解了。
Mybatis的配置文件有xml文件格式和注解格式,使用@Mapper注解的时候,Spring程序在运行时,Mybatis就会自动找到使用@Mapper注解的mapper,在编译的时候动态生成代理类。所以在service层能够注入成功。
使用@Repository注解也是可以的,只需要加入扫描注解即可。
@SpringBootApplication
@MapperScan(basePackages = "com.example.demo.Three.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}