SoFunction
Updated on 2025-04-06

Solve that mapper automatic assembly cannot be recognized, may not beans of ‘UserMapper’ type found

The mapper automatic assembly cannot be recognized, and could not beans of ‘UserMapper’ type found

After using MyBatisX plug-in to generate code using MybatisPlus, an error was reported when using automatic injection:

Could not autowire. No beans of ‘UserMapper’ type found

reason

Spring has no corresponding class found

Method 1: Add @MapperScan annotation

Add @MapperScan ("mapper folder package") annotation on the main configuration class or main boot class of the Spring Boot application

Allow Spring to scan in the corresponding package

@SpringBootApplication
@MapperScan("")
public class MiaoshaApplication {

    public static void main(String[] args) {
        (, args);
    }
}

Note:

  • First make sure the path is fine
  • The mapper file is a path of "" in both the development path and the traget export path

Method 2: Add @Repository

If the method is invalid as soon as it is used, this is the problem of idea. There will be no errors when running. If you want to remove the popularity, you can add the ignorance prompt Suppress. This idea can be automatically added in Alt+Enter

But I like to add @Repository to the Mapper class and tell idea to recognize directly.

@Repository
public interface UserMapper extends BaseMapper<User> {

}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.