0--前言
@Mapper和@Repository是常用的兩個注解,兩者都是用在dao上,兩者功能差不多,容易混淆,有必要清楚其細微區別;
1--區別
@Repository需要在Spring中配置掃描地址,然后生成Dao層的Bean才能被注入到Service層中:如下,在啟動類中配置掃描地址:
@SpringBootApplication //添加啟動類注解 @MapperScan("com.anson.dao") //配置mapper掃描地址 public class application { public static void main(String[] args) { SpringApplication.run(application.class,args); } }
@Mapper不需要配置掃描地址,通過xml里面的namespace里面的接口地址,生成了Bean后注入到Service層中。
也就是@Repository多了一個配置掃描地址的步驟;