不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-05-22 22:12:36  修改时间:2024-04-25 02:23:19  分类:Java框架/系统  编辑

在用Idea写一个实现类时引用了mapper类的来调用dao层的处理,使用@Autowired注解时被标红线,找不到bean。

 解决办法:

在mapper加注解@mapper或者@repository

这两种注解的区别在于:

1、使用@mapper后,不需要在Spring配置中设置扫描地址,通过mapper.xml里面的namespace属性对应相关的mapper类,Spring将动态的生成Bean后注入到ServiceImpl中。

@Mapper
public interface BlogSortMapper {
    int deleteByPrimaryKey(String sortId);

    int insert(BlogSort record);
}

2、@repository则需要在Spring中配置扫描包地址,然后生成dao层的bean,之后被注入到ServiceImpl中,如下面的Mapper扫描注解:@MapperScan

@MapperScan("com.wenjianbao.mapper")
@SpringBootApplication(scanBasePackages = "com.wenjianbao")
public class RunApplication {

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

}
@Repository
public interface BlogMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Blog record);
}

即:@repository@MapperScan是需要成对配合使用的,,,推荐使用注解 @mapper