@MapperScan和@Mapper共存问题

在一个项目中@MapperScan和@Mapper共存问题

测试案例

目录结构

mapper-test目录结构
service类中注入所有Mapper接口;
mapper-test-Service类

  1. 仅在Mapper接口上使用@Mapper注解,程序正常启动;
  2. 仅在启动类上使用@MapperScan({“com.example.mappertest.mapper”})注解,且mapper接口位于mapper文件夹下,项目成功启动;
  3. Mapper接口位于mapper文件夹下,使用@Mapper注解,并且启动类上使用@MapperScan({“com.example.mappertest.mapper”})注解,项目成功启动;
  4. Mapper接口移动到mapper1文件夹下,使用@Mapper注解,并且启动类上使用@MapperScan({“com.example.mappertest.mapper”})注解,项目无法启动;
  5. UserMapper接口移动到mapper1文件夹下,使用@Mapper注解,并且启动类上使用@MapperScan({“com.example.mappertest.mapper”})注解,项目无法启动;
    报错:Consider defining a bean of type 'com.example.mappertest.mapper1.UserMapper' in your configuration.

结论

@MapperScan和@Mapper同时存在时,@MapperScan 注解优先级高,@Maper 注解失效。