今天使用IDEA 搭建SpringBoot 项目,我心血来潮想省略Service 层的编写就直接在Controller 层调用Mapper 层接口,提示如下错误信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.zzg.controller.FirstController required a bean of type 'com.zzg.mapper.UserMapper' that could not be found.
错误原因:*Mapper 没有实列化
解决办法:1、每个mapper 接口使用@Component 或者@Repository 注解标签实列化Mapper
2、使用@MapperScan("mapper 接口路径地址") 扫描实现Mapper接口实例化
我采用方法2 实现。
package com.zzg;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 1.0.0 默认:1.0.0
* zzg 作者信息,可在通用配置里修改作者信息
* 2022/04/19 日期信息,格式可在通用配置中修改
* 应用程序开始 注释信息
**/
@SpringBootApplication
@MapperScan("com.zzg.mapper")
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class, args);
}
}
版权声明:本文为zhouzhiwengang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。