@Mapper注解和@MapperScan注解的使用

后端 1 521
小小草
小小草 2023年3月4日 23:53 发表

@MapperScan

如果想要每个接口都要变成实现类,那么需要在每个接口类上加上@Mapper注解,比较麻烦,解决这个问题用@MapperScan;
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类;
添加位置:是在Springboot启动类上面添加;

@SpringBootApplication
@MapperScan("com.winter.dao")
public class SpringbootMybatisDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
}
}
  • 添加@MapperScan(“com.winter.dao”)注解以后,com.winter.dao包下面的接口类,在编译之后都会生成相应的实现类。

3、使用@MapperScan注解多个包

  • 实际用的时候根据自己的包路径进行修改
  • @SpringBootApplication  
    @MapperScan({"com.kfit.demo","com.kfit.user"})
    public class App {
    public static void main(String[] args) {
    SpringApplication.run(App.class, args);
    }
    }

    4、Dao接口类没在Spring Boot主程序可以扫描的包或者子包下面

    • 可使用如下方式进行配置(没验证过,不确定能否使用,或许需要根据自己定义的包名进行修改路径):
    • @SpringBootApplication  
      @MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"})
      public class App {
      public static void main(String[] args) {
      SpringApplication.run(App.class, args);
      }
      }



点赞 0 收藏(0)    分享
相关标签: Mapper,MapperScan
问题没解决?让chatGPT帮你作答 智能助手
1 个评论
  • 邮箱用户_x3m7y
    2023年3月4日 20:55
    你好,请问可以加QQ:3578629253,请教一些关于论坛设计的问题吗
    0 0