In development basedSpring BootandMyBatisWhen applying it, we often encounter two very commonly used annotations:@Mapper
and@MapperScan
. The main function of these two annotations is to help the MyBatis framework identify and manage the Mapper interface, but they have different usage methods and applicable scenarios in actual applications.
This article will analyze in depth@Mapper
and@MapperScan
The difference and usage method help everyone better understand their role and make better choices in actual development.
1. @Mapper annotation
@Mapper
It is an annotation provided by MyBatis to mark the Mapper interface. It tells MyBatis that the interface is a mapper, and MyBatis will automatically generate the corresponding implementation class and enable it to be managed by Spring through dependency injection.
How to use
Add directly to the Mapper interface@Mapper
Just annotate:
import ; @Mapper public interface UserMapper { User findById(int id); List<User> findAll(); }
@Mapper
The role of
-
Tag interface:
@Mapper
Annotation marks this interface is a MyBatis Mapper interface, and MyBatis will automatically generate proxy objects for this interface. -
Leave it to Spring Management:pass
@Mapper
, the interface will be scanned by the Spring container and managed as a bean. Spring is responsible for injecting the data source into the corresponding SQL session and completing SQL operations. -
Concise and clear: Each Mapper interface needs to be marked
@Mapper
Annotations are concise and direct.
Use scenarios
- If there are fewer Mapper interfaces in the project and no batch scanning of the Mapper interface is required, use
@Mapper
Annotation is very straightforward and simple on each Mapper interface. - Suitable for small projects or when there are no special requirements for Mapper registration.
2. @MapperScan annotation
@MapperScan
Is annotation provided by Spring to batch scan all Mapper interfaces under the specified package path. pass@MapperScan
Annotation, Spring will automatically scan and register all matching Mapper interfaces, eliminating the addition of each interface.@Mapper
Trouble with annotation.
How to use
Add on the main class or configuration class of Spring Boot@MapperScan
Annotation, specify the path to the package to scan:
import ; import ; import ; @SpringBootApplication @MapperScan("") // Batch scans all Mapper interfaces under the package pathpublic class Application { public static void main(String[] args) { (, args); } }
@MapperScan
The role of
-
Batch scan:pass
@MapperScan
Specify a package path, and Spring will automatically scan all Mapper interfaces under the package and register it as a Mapper for MyBatis. -
Simplify configuration: Reduce individual additions on each interface
@Mapper
The annotation is complicated and the Mapper interface can be managed more centrally. -
Flexible configuration: If the project has multiple Mapper packages, multiple can be used
@MapperScan
Annotation specifies different package paths.
Use scenarios
- When there are multiple Mapper interfaces in the project, use
@MapperScan
It can avoid adding to each Mapper interface@Mapper
Annotations make the code more concise. - Suitable for medium and large projects, especially when multiple packages manage multiple Mapper interfaces,
@MapperScan
It can provide more efficient management methods.
3. Comparison between @Mapper and @MapperScan
characteristic | @Mapper |
@MapperScan |
---|---|---|
Range of action | Tagged on a single Mapper interface | Batch scans all Mapper interfaces under specified packages |
Use scenarios | There are fewer Mapper interfaces, or you need to control Mapper registration separately | There are many Mapper interfaces, so you want batch management |
Code simplicity | Each interface needs to be marked@Mapper annotation |
Just mark the startup class or configuration class once |
flexibility | Each Mapper interface can be finely controlled | Batch scanning, suitable for large-scale Mapper management |
4. How to choose in actual development
-
Small project or simple scenario: If you have only a few Mapper interfaces in your project and do not involve complex package structures, you can use
@Mapper
Annotations are simple and direct on each Mapper interface. -
Medium- and large-scale projects or complex scenarios: If there are a large number of Mapper interfaces in the project and these interfaces are distributed in multiple packages, use
@MapperScan
It will be more convenient and avoid adding it to each interface@Mapper
Annotation can also make it easier to manage and configure package paths.
Example: Mapper scan under multiple packages
Suppose there are multiple packages in the project that contain different Mapper interfaces:
@MapperScan("") // Scan the User-related Mapper@MapperScan("") // scanning Order Related Mapper
In this case, you can specify scan paths for different modules or fields respectively to make the project structure clearer.
5. Summary
@Mapper
and@MapperScan
These are all very important annotations when integrating MyBatis and Spring Boot. They have their own characteristics and applicable scenarios:
-
@Mapper
The annotation is suitable for situations where there are fewer Mapper interfaces or require manual management interfaces. It is simple and clear, and has a high granularity in control. -
@MapperScan
Annotations are suitable for situations where there are many Mapper interfaces or require batch scanning interfaces, simplifying configuration and management.
Depending on the complexity and needs of the project, choosing the right way to manage your Mapper interface can improve development efficiency and make your code more concise. I hope this article can help you understand better@Mapper
and@MapperScan
The use of the code improves the quality and maintainability of the code in development.
References
Spring Boot MyBatis official documentation
MyBatis official documentation
This is the article about the difference and use analysis of @Mapper and @MapperScan in MyBatis. For more related contents of MyBatis, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!