Problems with Mapper interface injection failure when Springboot integrated MyBatis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.