MyBatis Source Summary--Quick Start

Source: Internet
Author: User

First of all, I would like to summarize the code on the basis of learning the lakeside Breeze "MyBatis".

"MyBatis" http://blog.csdn.net/hupanfeng/article/details/9068003

One, mybatis parsing configuration file

The Sqlsessionfactorybuilder class is the portal to the mybatis, which resolves the configuration file when the Sqlseesionfactory is created and saves the information in the file to the configuration object.

Where the class Xmlconfigurebulider class, call the parse method of the Xpathparser class, get the configuration root node and call the Parseconfiguration method, Xpathparser is the core class for parsing XML

Public Configuration Parse () {    if (parsed) {      throw new Builderexception ("Each xmlconfigbuilder can is only is used on CE. ");    }    parsed = true;    Parseconfiguration (Parser.evalnode ("/configuration"));    return configuration;  }
parseconfigurationMethod is equivalent to a broker, which acquires different sub-nodes under the configuration, and calls the appropriate method to parse

 private void Parseconfiguration (xnode root) {try {propertieselement (Root.evalnode ("Properties"));//issue #117 Read Propertie      s first typealiaseselement (Root.evalnode ("typealiases"));      Pluginelement (Root.evalnode ("plugins"));      Objectfactoryelement (Root.evalnode ("objectfactory"));      Objectwrapperfactoryelement (Root.evalnode ("objectwrapperfactory"));      Settingselement (Root.evalnode ("Settings")); Environmentselement (Root.evalnode ("Environments")); Read it after Objectfactory and objectwrapperfactory issue #631 databaseidproviderelement (Root.evalnode ("Databasei      Dprovider "));      Typehandlerelement (Root.evalnode ("typehandlers"));    Mapperelement (Root.evalnode ("mappers")); } catch (Exception e) {throw new Builderexception ("Error parsing SQL Mapper Configuration.    Cause: "+ E, E); }  }
The Evalnode method is used in both methods to return the XNode class, where the Evaluate method obtains the target node and creates the XNode class

  Public XNode Evalnode (Object root, String expression) {    node node = (node) evaluate (expression, root, XPATHCONSTANTS.N ODE);    if (node = = null) {      return null;    }    return new XNode (this, node, variables);  }
The constructor method in xnode, which resolves the node properties and all child nodes
Construction Method:
</pre><pre name= "code" class= "java" > Public  xnode (xpathparser Xpathparser, node node, Properties Variables) {    this.xpathparser = Xpathparser;    This.node = node;    THIS.name = Node.getnodename ();    This.variables = variables;    This.attributes = parseattributes (node);    This.body = parsebody (node);  }

Parsing properties:

Private Properties Parseattributes (Node N) {    Properties attributes = new properties ();    NamedNodeMap attributenodes = N.getattributes ();    if (attributenodes! = null) {for      (int i = 0; i < attributenodes.getlength (); i++) {        Node attribute = attribute Nodes.item (i);        String value = Propertyparser.parse (Attribute.getnodevalue (), variables);        Attributes.put (Attribute.getnodename (), value);      }    }    return attributes;  }
Get all child nodes:

private string Parsebody (node node) {    String data = getbodydata (node);    if (data = = null) {      NodeList children = Node.getchildnodes ();      for (int i = 0; i < children.getlength (); i++) {        Node child = Children.item (i);        data = Getbodydata (child);        if (data! = null) break;      }    }    return data;  }

Summary: (1) Understand the parsing process by learning the source code

(2) Learn the idea of parsing XML programming. MyBatis design parsing XML conforms to the open and closed principle, if later configuration file to add a new sub-node under the Config node, only need to add the corresponding analytic method, the other places basically do not need to modify. If the contents of the existing child nodes are modified, only the corresponding methods of the different child nodes can be modified, and no other parsing node methods will be affected.

Ii. sqlsession operation of the database

In the "MyBatis in layman's words" said sqlsession to the database operation as follows:

    Sqlsession session= sqlsessionfactory.opensession ();    Userdao Userdao = Session.getmapper (userdao.class);    Userdto user = new Userdto ();    User.setusername ("Imybatis");    list<userdto> users = userdao.queryusers (user);
where Session.getmapper (userdao.class) Gets the proxy object for Userdao

Get the factory class that created the proxy object first

Public <T> T Getmapper (class<t> type, sqlsession sqlsession) {    final mapperproxyfactory<t> Mapperproxyfactory = (mapperproxyfactory<t>) knownmappers.get (type);    if (mapperproxyfactory = = null)      throw new Bindingexception ("type" + Type + "is not known to the Mapperregistry.");    try {      return mapperproxyfactory.newinstance (sqlsession);    } catch (Exception e) {      throw new Bindingexception ("Error getting mapper instance. Cause: "+ E, E);    }  }
In the factory class, create a proxy object

  Protected T newinstance (mapperproxy<t> mapperproxy) {    return (t) proxy.newproxyinstance ( Mapperinterface.getclassloader (), new class[] {mapperinterface}, Mapperproxy);  }  Public T newinstance (sqlsession sqlsession) {    final mapperproxy<t> mapperproxy = new Mapperproxy<t> ( Sqlsession, Mapperinterface, Methodcache);    Return newinstance (Mapperproxy);  }
In the proxy class, when the target method is executed, the method is saved to Cachemappermethod for later invocation.
public object invoke (object proxy, Method method, object[] args) throws Throwable {    if (Object.class.equals (method.ge Tdeclaringclass ()) {      return Method.invoke (this, args);    }    Final Mappermethod Mappermethod = Cachedmappermethod (method);    Return Mappermethod.execute (sqlsession, args);  }
Summary: (1) familiar with the process

(2) The code is not difficult, mainly through the learning source review the JDK dynamic agent, reflection and other related basic knowledge.


MyBatis Source Summary--Quick Start

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.