软件编程
位置:首页>> 软件编程>> java编程>> Mybatis中 mapper-locations和@MapperScan的作用

Mybatis中 mapper-locations和@MapperScan的作用

作者:普通网友  发布时间:2023-07-13 08:08:34 

标签:Mybatis,locations,@MapperScan

SpringBoot集成Mybatis时mybatis.mapper-locations和@MapperScan的作用

1、mybatis.mapper-locations的作用

mybatis.mapper-locations在SpringBoot配置文件中使用,作用是扫描Mapper接口对应的XML文件,如图中所示:扫描的是resources下的mapper文件夹中所有的xml结尾的文件。 _注:如果全程使用@Mapper注解,可以不使用该配置,即@Mapper可以替代Mapper接口对应的.xml文件,SpringBoot提倡“约定优于配置”。_*

Mybatis中 mapper-locations和@MapperScan的作用

我们可以点进去,可以看到是@ConfigurationProperties修饰的类,它的作用是将配置文件中的属性映射到对象中

Mybatis中 mapper-locations和@MapperScan的作用

此处是会将配置文件中以mybatis前缀的属性映射到对应字段上,即将mybatis.mapper-locations通过set方法映射到MapperLocations属性上

Mybatis中 mapper-locations和@MapperScan的作用

Mybatis中 mapper-locations和@MapperScan的作用

2、@MapperScan的作用

@MapperScan写在SpringBoot的启动类上(xxxApplication.java),作用是扫描Mapper接口类。

Mybatis中 mapper-locations和@MapperScan的作用

点进去可以看到@MapperScan会注册BeanDefinition到BeanFactory中,即生成接口对应的实现类。具体怎么实现不在此处赘述,有兴趣可以自行查看源码

Mybatis中 mapper-locations和@MapperScan的作用

@Retention作用是定义被它所注解的注解保留多久 @Target表明注解的作用目标 Documented注解表明这个注解应该被 javadoc工具记录 @Import注解就是之前xml配置中的import标签,可以用于依赖第三方包中bean的配置和加载 @Repeatable注解表明标记的注解可以多次应用于相同的声明或类型

总结:

1、mybatis.mapper-locations在SpringBoot配置文件中使用,作用是扫描Mapper接口对应的XML文件

2、@MapperScan会扫描Mapper接口类,并生成对应的实现类。

来源:https://blog.csdn.net/m0_67401761/article/details/123669619

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com