@MapperScan
is a Spring Boot annotation provided by MyBatis and MyBatis-Plus, used to automatically scan and register the Mapper interface, so that it can be managed by Spring containers and bound to the corresponding XML or annotated SQL. Its core role is to simplify the configuration of the MyBatis Mapper interface and avoid manually declaring one by one.
1. Basic usage
(1) Add @MapperScan to the startup class
@SpringBootApplication @MapperScan("") // Specify the package where the Mapper interface residespublic class MyApplication { public static void main(String[] args) { (, args); } }
Function: Spring will scanAll Mapper interfaces under the package and its subpackages are automatically registered as beans.
(2) Scan multiple packets
@MapperScan({"", ""})
Multiple package paths can be passed in, suitable for situations where Mappers are scattered in different modules.
2. The underlying principle of @MapperScan
When Spring starts,
@MapperScan
Will triggerMapperScannerRegistrar
, scan the interface under the specified packet.Generate proxy objects (via JDK dynamic proxy or CGLIB) for each Mapper interface and register with Spring container.
The proxy object will bind the corresponding SQL (XML or annotation method) to perform database operations.
3. Do you have to need @MapperScan?
1. Under what circumstances can you not use @MapperScan?
(1) Manual registration using MyBatis' <mapper> interface
If you are in the MyBatis global configuration file (such as) The Mapper interface is manually registered, for example:
<mappers> <mapper class=""/> </mappers>
No need@MapperScan
. But this method is rarely used in Spring Boot.
(2) Use @Mapper annotation to mark each DAO interface
If each Mapper interface is added@Mapper
Annotations (annotations provided by MyBatis), Spring Boot will automatically scan them:
@Mapper // Key notespublic interface UserDao { User selectById(Long id); }
No need at this time@MapperScan
, but make sure:
- The package path where the interface resides is scanned by the Spring Boot main class by default (i.e., the same level or subpackage as the startup class).
- There are no other conflicting configurations in the project.
2. Under what circumstances do you have to use @MapperScan?
(1) Not used @Mapper annotation
If the DAO interface is not added one by one@Mapper
Annotation must be passed@MapperScan
Specify the scan path in batches:
@SpringBootApplication @MapperScan("") // Specify the package where the DAO interface residespublic class App { public static void main(String[] args) { (, args); } }
(2) Requires flexible control of the scanning range
When the DAO interface is spread across multiple packets:
@MapperScan({"", ""})
When certain interfaces need to be excluded (in conjunction with custom filters).
This is the end of this article about SpringBoot annotation @MapperScan implementation. For more related SpringBoot @MapperScan content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!