SoFunction
Updated on 2025-03-10

Analysis of the difference between @Mapper and @MapperScan in MyBatis

In development basedSpring BootandMyBatisWhen applying it, we often encounter two very commonly used annotations:@Mapperand@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@Mapperand@MapperScanThe difference and usage method help everyone better understand their role and make better choices in actual development.

1. @Mapper annotation

@MapperIt 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@MapperJust annotate:

import ;
@Mapper
public interface UserMapper {
    User findById(int id);
    List<User> findAll();
}

@MapperThe role of

  • Tag interface@MapperAnnotation 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@MapperAnnotations 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@MapperAnnotation 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

@MapperScanIs annotation provided by Spring to batch scan all Mapper interfaces under the specified package path. pass@MapperScanAnnotation, Spring will automatically scan and register all matching Mapper interfaces, eliminating the addition of each interface.@MapperTrouble with annotation.

How to use

Add on the main class or configuration class of Spring Boot@MapperScanAnnotation, 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);
    }
}

@MapperScanThe role of

  • Batch scan:pass@MapperScanSpecify 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@MapperThe 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@MapperScanAnnotation specifies different package paths.

Use scenarios

  • When there are multiple Mapper interfaces in the project, use@MapperScanIt can avoid adding to each Mapper interface@MapperAnnotations make the code more concise.
  • Suitable for medium and large projects, especially when multiple packages manage multiple Mapper interfaces,@MapperScanIt 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@Mapperannotation 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@MapperAnnotations 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@MapperScanIt will be more convenient and avoid adding it to each interface@MapperAnnotation 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

@Mapperand@MapperScanThese are all very important annotations when integrating MyBatis and Spring Boot. They have their own characteristics and applicable scenarios:

  • @MapperThe 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.
  • @MapperScanAnnotations 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@Mapperand@MapperScanThe 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!