Problem: if you use @ repository annotation to annotate Dao layer alone, and do not use @ mapperscan scan, you will report an error when starting the project: field xxmapper in com.sms.shiro.service.impl.xxserviceimpl required a bean of type ‘com.sms.shiro.mapper.xxmapper’that could not be found.
, the difference between @ mapper and @ Repository:
1, @Repository is the annotation of spring family
2. When using @ mapper annotation, spring doesn’t know it, and the mapper injected into service by @ Autowired will be popular.
2、 Back to the question, it is clear that componentscan in springboot scans all annotated components under the starter package, but why can’t we find @ repository components
reason: the reason for this problem is that springboot automatically filters out interfaces and abstract classes during scanning, so the mapper interface modified by @ repository can’t be called a bean, so it can’t be injected into the service.
3、 Solution:
3.1 do not use @ repository, use @ mapper directly in the mapper layer interface, but if the project is large, it will be very troublesome.
3.2 if you want to use @ repository because springboot can’t scan mapper interface, then use @ mapperscan to scan all mapper interface packages on the launcher, such as: @ mapperscan ("com. SMS. Shiro. *. Mapper")
3.3 in fact, after using @ maperscan annotation to scan the mapper interface, the mapper interface does not need @ repository and @ mapper to register beans. However, if you want the project structure to be clear, after using @ maperscan annotation to scan, both @ repository and @ mapper can be used, and no error will be reported.
Read More:
- Solve the problem that the delete request is not available under SpringBoot. There was an unexpected error (type=Method Not Allowed, status=405).
- Solve the problem of springboot and unit test starting applicationrunner
- Yum error: cannot retrieve repository metadata( repomd.xml )For repository solution
- Spring boot can’t scan XML (invalid bound statement (not found))
- Fedora 14 yum Error: Cannot retrieve repository metadata (repomd.xml) for repository
- How to solve the problem of artifact contains illegal characters when creating springboot project with idea
- The scan of annotations for web application [] could not be completed due to a stackoverflower error. Possible root causes include the low setting of – XSS and illegal circular inheritance dependencies. The class hierarchy being processed is
- Error: Cannot retrieve repository metadata (repomd.xml) for repository: c6-media. Please verify…
- CentOS 8.2 installation-solve Error setting up base repository
- Ubuntu add apt repository command not found solution
- Solve the problem of request method ‘get’ not supported
- Springboot project startup exception – required a single bean, but 2 were found
- Host key verification failed. fatal: Could not read from remote repository.
- Arduino reports an error when writing a custom library file to solve the problem of not name of type, not declared in this scope
- Solve the problem of MySQL 1251 client does not support
- Springboot time zone problem
- Dirsearch URL batch scan error
- How to solve the problem of error 15: file not found when Linux starts
- Solve the problem of flag error valueerror: View function did not return a response
- Solution of redisconnection factory in springboot2 not Autowired