MyBatis Spring mapperscannerconfigurer Configuration

Source: Internet
Author: User

It is not necessary to register all the mappers in the Spring XML configuration file. Instead, you can use a mapperscannerconfigurer, which will look for mappers under the classpath and automatically build them into Mapperfactorybean.

To create a mapperscannerconfigurer, you can add the following code to the Spring configuration:

<class= "Org.mybatis.spring.mapper.MapperScannerConfigurer">  <name= "Basepackage"  value= " Org.mybatis.spring.sample.mapper "/></bean>

The Basepackage property lets you set the basic package path for the Mapper interface file. You can use a semicolon or comma as a delimiter to set more than one package path. Each mapper will be recursively searched in the specified package path.

The Mapperscannerconfigurer property does not support the use of Propertyplaceholderconfigurer property substitution because it is loaded before Spring. However, you can use Propertiesfactorybean and Spel expressions as overrides.

Note that it is not necessary to specify Sqlsessionfactory or Sqlsessiontemplate, because Mapperscannerconfigurer will create Mapperfactorybean and then assemble automatically. However, if you use more than one DataSource, automatic assembly may fail. in this case, you can use the Sqlsessionfactorybeanname or Sqlsessiontemplatebeanname property to set the correct bean name. This is how it is configured, note that the bean name is required, not the bean's reference, and therefore the Value property replaces the usual ref:

<name= "Sqlsessionfactorybeanname"  value= "Sqlsessionfactory"  />

Mapperscannerconfigurer supports filtering by creating a mapper from the specified creation interface or annotations. The Annotationclass property specifies the name of the note to look for. The Markerinterface property specifies the parent interface to look for. If both are specified, the mapper added to the interface will match the two criteria. By default, both properties are null, so all interfaces given in the base package can be loaded as mappers.

The discovered mapper will use spring to name the auto-detect component (refer to the Spring Manual's 3.14.4) default naming policy. That is, if no annotations are found, it uses the non-uppercase, non-fully qualified class name of the mapper. However, if a @named annotation of @component or JSR-330 is found, it gets the name. Note that you can configure to Org.springframework.stereotype.Component, javax.inject.Named (if you use JSE 6) or your own annotations (which are definitely self-explanatory), which The sample annotations will be used as generators and name providers.

MyBatis Spring mapperscannerconfigurer Configuration

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.