在yml中配置了mybatis,但是却报错找不到mapper,如下图?

问题

按网上的搜索,配置@Mapper依然报错

Description:

Field tagMapper in com.example.demo.service.TagService required a bean of type 'com.example.demo.mapper.TagMapper' 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.mapper.TagMapper' in your configuration.


Process finished with exit code 1

代码

github

相关信息

报错信息

1680685687989.png

controller和service

1680685829149.png

bean和mapper

1680685853198.png

阅读 2.6k
2 个回答

一般得加MapperScann告诉容器去扫描哪个包,但是新版本的springboot已经做了默认配置,不加就是扫描启动类所在包以及子路径,代码地址在哪,给你看看

在运行类上添加@MapperScan注解 @MapperScan("com.example.demo.mapper")

@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

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

}

并且在你的Mapper的@Mapper基础上再加上一个@Repository注解

@Repository
@Mapper
public interface TagMapper{
}

这样来把它定义为一个bean注入到Service里

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题