mybatis的@MapperScan注解和@Mapper的区别

一、@MapperScan

它用于标注在SpingBoot的启动类上面,然后用MapperScan类的basePackages()属性或直接进行编写包的扫描范围,来扫描该路径下的所有接口。

String[] basePackages() default {};

例如:
1.直接赋值范围

@MapperScan("com.hmdp.mapper")
@SpringBootApplication
public class PingApplication {
    public static void main(String[] args) {
        SpringApplication.run(PingApplication.class, args);
    }
}

2.根据属性复制范围

@MapperScan(basePackages = "com.hmdp.mapper")
@SpringBootApplication
public class PingApplication {
    public static void main(String[] args) {
        SpringApplication.run(PingApplication.class, args);
    }
}

二、@Mapper

用来标记在接口中,表明该标记类是一个mapper接口。
例如:

@Mapper
public interface CommentsMapper {

}

总结:

  • 相同点:都是用来注入spring容器的。
  • 不同点:@mapper 只可以有标注该注解的才可以注入进去(如果有多个mapper接口则工作量比较多)。@MapperScan 可以批量进行注入只要标明包的路径该路径下的所有包都可以扫描注入进去。

注:如果某个mapper类没有标注@Mapper注解并且该类也被其他的注入类依赖,或者@MapperScan没有加,启动时就会报错

No qualifying bean of type 'com.hmdp.mapper.BlogCommentsMapper' 
available: expected at least 1 bean which qualifies as autowire 
candidate. Dependency annotations:{@org.springframework.beans
.factory.annotation.Autowired(required=true)}

如有问题或意见请指出,谢谢!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值