The difference between @Mapper and @MapperScan annotations in SpringBoot integrated Mybatis

I. Introduction

Using SpringBootintegration Mybatistime of persistence framework for the Mybatisinterface class is generated *Mapper, the interface classes *Mapper, which acts as the Daorole of the layer, and therefore a need to be labeled Beanis injected into the Springcontainer, to Springbe managed. There are two ways to inject into Spring container management. For example, I have one ProductMapperand one OrderMapper:

  • Method 1: Add a @Mapper annotation to both ProductMapper and OrderMapper interface classes
  • Method 2: Add a @MapperScan to the SpringBoot startup class to specify the ProductMapper and OrderMapper class paths to be scanned.

The following describes the functions and implementation methods of the two annotations, as well as their advantages and disadvantages.

Two, @Mapper annotation

Use:*Mapper Annotate on a single interface class , the corresponding interface implementation class will be generated after compilation, and the Bean will be injected into the Spring container.
Location: marked on a corresponding interface class.
Disadvantages:*Mapper @Mapper annotations need to be marked on each interface class , which is very cumbersome, and this is a repetitive and useless work.

 

Guess you like

Origin blog.csdn.net/Thinkingcao/article/details/108124361