background

The project the author participated in uses multi-module structure, different modules use SpringBoot, and the basic structure is similar. When we checked a problem recently, we found two modules, one does not use @mapperscan, the other uses this annotation, but the Mapper class of MyBatis can be correctly scanned.

Unified code structure, remove the @mapperscan annotation, the corresponding module reported an error, indicating that the injection DAO class failed. Then we compared the next two projects, tracked the usage of the two annotations, and found the root of the problem. This article Outlines the process.

MapperScan Indicates the scanning mode

The @mapperscan annotation will encapsulate all DAO classes in the specified directory into MyBatis BaseMapper class, and then inject it into the Spring container. No additional annotations are required to complete the injection. Common DAO definitions are as follows:

package cn.xxx.xxx.xxx.dao;
public interface IXXXDao  extends BaseMapper<XXX>{}
Copy the code

Note that in addition to MyBatis DAO class, there should be no custom ordinary DAO interface and its implementation class in @mapperscan scanning path.

For example, in this project, the DAO directory is largely inheritedBaseMapperDAO, but there is also a custom plain DAO interface and an implementation class for it under the IMPL.The project startup class uses the scan annotation:

@MapperScan({"cn.com.xx.xxmodule.dao"})
Copy the code

Project startup error:Visible not inherited fromBaseMapperCommon interfaces and their implementation classesWere encapsulated as Mapper class injection, resulting in the failure of dependency injection of the Service class referencing the DAO.

There are two ways around this:

  1. The custom common interface class is placed separately to distinguish the MyBatis DAO from the engineering custom DAO.
  2. Add for the Impl implementation class@PrimaryInterfaces, the downside of which is that meaningless interface definitions are also wrapped as Mapper.

Mapper annotation method

MyBatis will automatically scan all @mapper annotations in the bootstrap class directory. The main log is:

The 10:01:31 2020-10-25, 850, the DEBUG (MybatisPlusAutoConfiguration. Java: 275) - the Searching for mappers annotated with @ Mapper The 10:33:08 2020-10-25, 200 the DEBUG (MybatisPlusAutoConfiguration. Java: 275) - Using auto - configuration base package 'cn.xx.xx.xx'Copy the code

The revelation of

There are two different methods to inject MyBatis DAO entities. Choose one of them. Both annotations exist in the project.

In fact, by using @mapperscan, the DAO class of the corresponding package does not need @Mapper; If you use @mapper, you don’t need @mapperscan.

In the project, almost all DAO classes use the @mappper annotation, and at the same time set the @Mapperscan annotation for the startup class. Since the @Mapperscan annotation has a high priority, the @Maper annotation will be invalid.

The root of this is probably that the code is copied and no one really knows what the difference is between different configurations. @mapperscan was created to solve the tedious process of writing @mapper to every class!