欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > @MapperScan

@MapperScan

2025/2/2 16:49:33 来源:https://blog.csdn.net/2301_80484340/article/details/144934346  浏览:    关键词:@MapperScan

简介:

@MapperScan注解是MyBatis框架在Spring Boot中的一个重要集成注解

作用:

@MapperScan主要作用是告诉Spring框架在启动时扫描指定的包路径,并将该路径下的所有MyBatis的Mapper接口批量注入到Spring容器中。这样,开发者就可以在Spring应用中直接通过@Autowired等方式注入Mapper实例,进行数据库操作,而无需在每个Mapper接口上单独添加@Mapper注解,从而简化了配置,提高了开发效率。

属性:

value/basePackages:用于指定需要扫描的包路径,可以是一个或多个包路径,支持使用通配符。
basePackageClasses:用于指定需要扫描的包路径的类,Spring会通过这个类的包路径来扫描Mapper接口。
annotationClass:用于指定需要扫描的接口上应该具有的注解类型,默认是@Mapper注解。
nameGenerator:用于指定Bean名称生成器,默认是org.mybatis.spring.mapper.BeanNameGenerator。
factoryBean:用于指定生成Mapper Bean的工厂类,默认是org.mybatis.spring.mapper.MapperFactoryBean。

举个栗子:

@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

@MapperScan("com.example.mapper")指定了com.example.mapper包下的所有接口都会被MyBatis扫描并注册为Mapper。

指定多个包路径:

@SpringBootApplication
@MapperScan({"com.example.mapper", "com.example.another.mapper"})
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com