前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >@MapperScan注解使用

@MapperScan注解使用

作者头像
全栈程序员站长
发布2022-07-02 10:42:47
3790
发布2022-07-02 10:42:47
举报

大家好,又见面了,我是你们的朋友全栈君。

1、@Mapper注解: 作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类 添加位置:接口类上面

@Mapper public interface UserDAO { //代码 }

如果想要每个接口都要变成实现类,那么需要在每个接口类上加上@Mapper注解,比较麻烦,解决这个问题用@MapperScan

2、@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); } } 原文:https://blog.csdn.net/nba_linshuhao/article/details/82783454

早点的时间是直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,比较麻烦。

现在通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如:

@SpringBootApplication @MapperScan(“com.lz.water.monitor.mapper”) // 添加对mapper包扫描 public class Application {

public static void main(String[] args) { SpringApplication.run(Application.class, args); }

}

同时,使用@MapperScan注解多个包

  1. @SpringBootApplication
  2. @MapperScan({“com.kfit.demo”,”com.kfit.user”})
  3. public class App {
  4. public static void main(String[] args) {
  5. SpringApplication.run(App.class, args);
  6. }
  7. }

如果如果mapper类没有在Spring Boot主程序可以扫描的包或者子包下面,可以使用如下方式进行配置

  1. @SpringBootApplication
  2. @MapperScan({“com.kfit.*.mapper”,”org.kfit.*.mapper”})
  3. public class App {
  4. public static void main(String[] args) {
  5. SpringApplication.run(App.class, args);
  6. }
  7. }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/130203.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档