1. The role of @Repository
@Repository is an annotation belonging to Spring. It is used to mark the access layer class (Dao layer), which represents a warehouse and is mainly used to encapsulate access to the database. Its implementation is the same as the @Component annotation, but it is only established to clarify the role of the class.
That is, @Repository is a derivative of the @Component annotation, and both @Service and @Controller can be understood as extensions of the @Component annotation. Their function is to instantiate beans on the class and hand over the implementation class of the current class object to the spring container for management.
In other words, which class is modified by the @Repository annotation indicates that this class has the function of CRUD for the database and is used in the interface of the persistence layer.
In addition, as an annotation of spring, it can also encapsulate data access exceptions thrown in the annotated class into spring's data access exception type.
2. What is the difference between @Repository, @Service and @Component?
The function of @Repository is as mentioned above.
The @Service annotation is used to annotate classes in the service layer and is used to process business logic. In classes marked with the @Service annotation, @Reposity classes are usually injected.
The @Component annotation is a general annotation used to mark all components managed by the spring container. In classes marked with the @Component annotation, classes marked with @Service and @Repository are usually injected.
Essentially, the instantiated objects are handed over to spring for management.
3. Similarities and differences between @Repository and @Mapper
@Mapper is an annotation belonging to mybatis. In the program, mybatis needs to find the corresponding mapper and dynamically generate the proxy class during compilation to implement the database query function.
The @Mapper and @Repository annotations are used in the same way, adding annotations to the interface of the persistence layer.
However, if you only use the @Mapper annotation alone, a warning will appear during automatic assembly in the idea, indicating that the bean cannot be found. However, this does not affect the operation of the program and can be ignored directly.
If you want to avoid this warning, you can ignore this warning in the idea settings, or you can use @Mapper at the same time.
@Repository annotation. In this way, spring will scan @Repository and identify this bean, and this warning will not appear.
** Under normal circumstances, we mostly use @Mapper instead of @Repository annotation. **
Without using the @Repository annotation, the implementation class that implements the injection of this interface mainly has the following three methods:
1. In the spring configuration file, the bean MapperScannerConfigure is configured. It will scan the persistence layer interface to create an implementation class and hand it over to spring for management.
2. The interface uses @Mapper annotation.
3. Use the @MapperScan annotation on the springboot startup class, which has the same effect as MapperScannerConfigure.
4. Use @Repository alone correctly
The @Repository annotation is an annotation used to mark components of the data access layer. It will be scanned by spring and injected into the ioc container. Even if the @MapperScan annotation or MapperScannerConfigurer is not used, it can take effect as long as the component marked by the @Repository annotation is the same as the mapper interface implementation of Mybatis.
However, there may be some problems using the @Repository annotation in mybatis. When using the @Repository annotation, spring will treat it as a spring component, that is, create a proxy object for this class and manage it in the ioc container. However, the mapper interface implementation in mybatis is not a component of spring. They are proxy objects created by mybatis. Using the @Repository annotation may cause the proxy object created by mybatis to be re-created by spring, causing problems.
Therefore, it is recommended to use the @MapperScan annotation or MapperScannerConfigurer in mybatis to scan mapper interface implementations and inject them into the ioc container instead of using the @Repository annotation.
5. Summary:
- @Repository is the annotation of spring, and @Mapper is the annotation of mybatis.
- Both @Repository and @Mapper can be used. They can appear at the same time or be used alone.
- When using @Repository alone, you need to use MapperScannerConfigurer or @MapperScan annotation.
- When using @Mapper alone, a warning may appear in the editor, but it does not affect program operation. You can use @Repository to eliminate warnings. (You can also set this warning in the editor to ignore it)