@Repository 与 @Mapper的区别
相同点:
@Mapper和@Repository都是作用在dao层接口,使得其生成代理对象bean,交给spring 容器管理
对于mybatis来说,都可以不用写mapper.xml文件
不同点:
(1)首先@mapper @mapperScan是属于ibatis体系下的,而@Repository是属于springframework体系下的
1、@Mapper不需要配置扫描地址,可以单独使用,如果有多个mapper文件的话,可以在项目启动类中加入@MapperScan(“mapper文件所在包”),但是为了让idea扫描到还是建议加@Mapper,虽然项目不会出错,不过idea一直爆红看着糟心的很
这样看的话这个@MapperScan 确实显得哈哈哈哈哈
2、@Repository不可以单独使用,否则会报错误,要想用,必须配置扫描地址(@MapperScannerConfigurer),或者用ibaits 体系下的@MapperScan把他扫描成Mapper
eg:
@Repository("costListDao")
public interface CostListMapper extends BaseMapper<CostList> {
/**
* 支持高级查询的接口
*/
List<CostList> queryCostListAll(@Param("name") String name,
@Param("teamId") Integer teamId);
}
@MapperScan("com.yanzhen.dao")
public class TeamProjectApplication {
public static void main(String[] args) {
SpringApplication.run(TeamProjectApplication.class, args);
}
}
3.解决使用@mapper接口时,注入mapper爆红问题
在idea中单独使用@Mapper注解,在@Autowired时,idea会提示找不到bean,但是不影响运行,如果想消除爆红,可以将@Mapper注解跟@Repository注解一起用,这样便可消除爆红
推荐:@Mapper注解跟@Repository注解一起用