Solve the problem of Could not autowire. No beans of 'UserDao' type found

Foreword: Today, when improving the project, I found that the Dao layer dependency injected by @Autowired reported an error, but it did not affect the operation of the project. Record it here

question

insert image description here
This error doesn't affect the project running, but it looks annoying...

analyze

Dao layer code:

@Mapper
public interface UserDao {
    
    

    User findUserById(@Param("userId") int userId);

    User findUserByNickname(@Param("nickname") String nickname);

    User login(String username,String password);
}

Now that the project can run, it proves that the code is correct. The @Mapper annotation has given the proxy class of the interface to the Spring container for management. In theory, no error should be reported.
It is suspected that the version of IDEA (2018 version) currently in use is a bit old , the @Mapper annotation is not recognized, and then when the project is automatically compiled, a red wavy line pops up (PS: I have never used the 2020 version of IDEA before. this error).

Solution

Add @Repository annotation to Dao layer interface (@Repository annotation is Spring's annotation, which actively identifies the current class to be managed by Spring container, and then generates Dao layer bean).



PS: You can also go to my personal blog to see more content
Personal blog address: Xiaoguan classmate's blog

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/123085510