Problem Description:
When the spring boot integration MyBatis, the Mapper interface has failed to inject until the boot is discovered.
The phenomenon is as follows:
Vehicledao is the desired mapper object, a simple interface.
The Mybatis.config-location=classpath:mybatis-config.xml has been configured in Application.properties.
The package that corresponds to the mapper is specified in Mybatis-config.xml and is configured as follows:
Think for a long time do not understand why Mybatis-config specified the mappers is located in the package, still cannot load the corresponding mnapper.
But add @mapper annotations directly to the interface or at the springboot entrance, adding @mapperscan specifies that the package to be scanned will start normally.
If you don't understand, just debug. To facilitate access to information, a class that implements the Beanfactoryaware is injected into the program. Then through beanfactory, we go to get the corresponding bean.
The corresponding bean does not exist when it is found that the corresponding mapper is obtained directly.
However, when the sqlsession is obtained, the corresponding mapper agent is found when the corresponding mapper is located from the sqlsession.
This is only remembered, mybatis-config only for the corresponding mapper to create a proxy class, and want to really package into beans, injected into the spring container, or need to rely on Autoconfiguredmapperscannerregistrar, It registers as a bean according to the scan @mapper annotation or the interface under the package specified by @mapperscan.
The previous problem was that I took it for granted that the configuration of mappers in Mybatis-config and the role of @mapper annotations were confused!
Problems with Mapper interface injection failure when Springboot integrated MyBatis