The principle of mybatic mapperscannerconfigurer

Source: Internet
Author: User

Original address: http://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html

Objective

This article will analyze the underlying principles of the mybatis and spring-integrated mapperscannerconfigurer, which have previously been analyzed for implementation in Java, and can be used to generate dynamic proxies using the JDK's own API and Cglib third-party libraries. This article analyzes the MyBatis version 3.2.7,mybatis-spring version 1.2.2.

Mapperscannerconfigurer Introduction

mapperscannerconfigureR is a class provided in the spring and MyBatis integrated mybatis-spring jar packages.

To understand the role of this class, you need to understand Mapperfactorybeanfirst.

The advent of Mapperfactorybean is implemented using dynamic proxies instead of manually writing code that uses Sqlsessiondaosupport or sqlsessiontemplate to write data Access Objects (DAO).

For example, the following configuration is in the official documentation:

<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >

<property name= "Mapperinterface" value= "Org.mybatis.spring.sample.mapper.UserMapper"/>

<property name= "sqlsessionfactory" ref= "Sqlsessionfactory"/>

</bean>

Org.mybatis.spring.sample.mapper.UserMapper is an interface, we create an Mapperfactorybean instance, and then inject this interface and Sqlsessionfactory (MyBatis provides the Sqlsessi Onfactory interface, Mapperfactorybean uses Sqlsessionfactory to create the sqlsession) two properties.

Later, if you want to use this Usermapper interface, inject the bean directly through spring, and then you can use it directly, and spring will create a dynamic proxy for this interface.

When it comes to using multiple mapperfactorybean, a definition must be very cumbersome, so mybatis-spring provides the Mapperscannerconfigurer class, It will look for the mapper under the Classpath and automatically create the Mapperfactorybean.

<bean Class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >

<property name= "Basepackage" value= "Org.mybatis.spring.sample.mapper"/>

</bean>

This configuration scans all the interfaces under Org.mybatis.spring.sample.mapper and then creates dynamic proxy classes for their respective interfaces.

Note: This will scan all of the following interfaces, but in my experiment, only interfaces configured in namespace in Mapper.xml will generate dynamic proxy classes. Does not affect other interfaces below the basepackage.

Mapperscannerconfigurer Underlying code Analysis

Use the following code to illustrate the example (some code, other code and configuration omitted):

Package Org.format.dynamicproxy.mybatis.dao;

Public Interface Userdao {

Public User getById(int id);

Public int Add (user user);

Public int Update (user user);

Public int Delete (user user);

Public List<user> getAll();

}

<bean Class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >

<property name= "Basepackage" value= "Org.format.dynamicproxy.mybatis.dao"/>

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

</bean>

Let's start by debugging the test case to see what the Userdao implementation class is.

As we can see,Userdao is an example of 1 mapperproxy classes.
Look at the source of the Mapperproxy, yes, the implementation of the Invocationhandler, indicating the use of the JDK's own dynamic agent.

Public class Mapperproxy<T> implementsInvocationhandler, Serializable {

Private Static Final long serialversionuid = -6424540398559729838l;

Private Final Sqlsession sqlsession;

Private Final Class<t> Mapperinterface;

Private Final Map<method, mappermethod> Methodcache;

Public Mapperproxy (sqlsession sqlsession, class<t> mapperinterface, Map<method, mappermethod> MethodCache) {

this. sqlsession = sqlsession;

this. mapperinterface = Mapperinterface;

this. Methodcache = Methodcache;

}

Public Object invoke(object proxy, Method method, object[] args) throws Throwable {

if (Object.class.equals (Method.getdeclaringclass ())) {

Try {

return Method.invoke (this, args);

} catch (Throwable t) {

Throw Exceptionutil.unwrapthrowable (t);

}

}

Final Mappermethod Mappermethod = Cachedmappermethod (method);

return mappermethod.execute (sqlsession, args);

}

Private Mappermethod Cachedmappermethod(method) {

Mappermethod Mappermethod = Methodcache.get (method);

if (Mappermethod = = null) {

Mappermethod = new Mappermethod (Mapperinterface, Method, Sqlsession.getconfiguration ());

Methodcache.put (method, Mappermethod);

}

return Mappermethod;

}

}

Note: Understanding the concept see here is possible, if you need detailed research can continue to look at the following content.

The following begins to analyze the source code of Mapperscannerconfigurer

The Mapperscannerconfigurer implements the Beandefinitionregistrypostprocessor interface, The Beandefinitionregistrypostprocessor interface is an interface that modifies a defined bean in the spring foreman, which has a postprocessbeandefinitionregistry method.

Then we look at the key in the Classpathmapperscanner is how to scan the corresponding package under the interface.

In fact, the role of Mapperscannerconfigurer is to transform the corresponding interface type into Mapperfactorybean, and this Mapperfactorybean attribute Mapperinterface is the original type. Mapperfactorybean the beginning of this article has been analyzed.

So in the end we still have to analyze the mapperfactorybean principle of implementation!

Mapperfactorybean inherits the Sqlsessiondaosupport class, Sqlsessiondaosupport class inherits Daosupport abstract class, The Daosupport abstract class implements the Initializingbean interface, so the Afterpropertiesset method of the Initializingbean interface is called when an instance of Mapperfactorybean occurs.

Daosupport Method of Afterpropertiesset:

Mapperfactorybean overrides the Checkdaoconfig method:

Then take the corresponding bean from the Spring factory:

The sqlsession here is the Getmapper method of Sqlsessiontemplate,sqlsessiontemplate:

The Getmapper method of the configuration uses the Mapperregistry Getmapper method:

Mapperregistry Method of Getmapper:

Mapperproxyfactory Construction Mapperproxy:

That's right! Mapperproxyfactory is the use of the JDK group with the proxy to complete the dynamic agent.
Mapperproxy was already mentioned at the beginning. Mapperproxy internally uses a call to the Mappermethod class completion method:

Below, we use Userdao's GetByID method to debug to see how Mappermethod's Execute method goes.

@Test

Public void Testget () {

int id = 1;

System. out. println (Userdao.getbyid (id));

}

<select id= "getById" parametertype= "int" resulttype= "Org.format.dynamicproxy.mybatis.bean.User" >

SELECT * FROM users WHERE id = #{id}

</Select>


The principle of mybatic mapperscannerconfigurer

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.