The use and principle analysis of mapper mapping configuration in MyBatis frame (ii) configuration Chapter Sqlsessionfactorybuilder,xmlconfigbuilder

Source: Internet
Author: User

The use and principle analysis of mapper mapping configuration in <mybatis framework (i) see the sessionfactory creation process in the demo of configuration and use >:

Sqlsessionfactory sessionfactory = null;        String resource = "Mybatisconfig.xml";        try {            sessionfactory = new Sqlsessionfactorybuilder (). Build (Resources                    . Getresourceasreader (Resource));        } catch (IOException e) {            e.printstacktrace ();        }

So let's start with Sqlsessionfactorybuilder and look at the MyBatis loading process.

Sqlsessionfactorybuilder's core Source:

 Package org.apache.ibatis.session;
public class Sqlsessionfactorybuilder {
Read MyBatis configuration through reader
 Public sqlsessionfactory Build (Reader Reader, String environment, properties properties) {
Try{xmlconfigbuilder parser=Newxmlconfigbuilder (reader, Environment, properties); returnBuild (Parser.parse ());//parse () method get configuration}Catch(Exception e) {ThrowExceptionfactory.wrapexception ("Error building sqlsession.", E); } finally{errorcontext.instance (). reset (); Try{reader.close (); } Catch(IOException e) {//intentionally ignore. Prefer previous error.      }    }  }
Read MyBatis configuration via InputStream Publicsqlsessionfactory Build (InputStream InputStream, String Environment, properties properties) {Try{xmlconfigbuilder parser=NewXmlconfigbuilder (InputStream, Environment, properties); returnBuild (Parser.parse ());//parse () method get configuration}Catch(Exception e) {ThrowExceptionfactory.wrapexception ("Error building sqlsession.", E); } finally{errorcontext.instance (). reset (); Try{inputstream.close (); } Catch(IOException e) {//intentionally ignore. Prefer previous error. } } }
The above 2 methods are ultimately called build (Configuration config) Publicsqlsessionfactory build (Configuration config) {return Newdefaultsqlsessionfactory (config); }}

Through the source code, we can see Sqlsessionfactorybuilder through the Xmlconfigbuilder to parse our incoming MyBatis configuration file, construct the config, and finally return to the new The sqlsessionfactory instance of the Defaultsqlsessionfactory (config).

Next we see how Xmlconfigbuilder parse mybatis configuration file, the following is part of the source code:

 PackageOrg.apache.ibatis.builder.xml;/*** Parse mybatis configuration file*/ Public classXmlconfigbuilderextendsBasebuilder {Private Booleanparsed; PrivateXpathparser parser; PrivateString Environment;  PublicXmlconfigbuilder (Reader reader) { This(Reader,NULL,NULL); }   PublicXmlconfigbuilder (Reader Reader, String environment) { This(Reader, Environment,NULL); }   PublicXmlconfigbuilder (Reader Reader, String environment, Properties props) { This(NewXpathparser (Reader,true, props,NewXmlmapperentityresolver ()), environment, props); }   PublicXmlconfigbuilder (InputStream inputstream) { This(InputStream,NULL,NULL); }   PublicXmlconfigbuilder (inputstream inputstream, String environment) { This(InputStream, Environment,NULL); }   PublicXmlconfigbuilder (inputstream inputstream, String Environment, Properties props) { This(NewXpathparser (InputStream,true, props,NewXmlmapperentityresolver ()), environment, props); }  PrivateXmlconfigbuilder (xpathparser parser, String environment, Properties props) {Super(NewConfiguration ()); Errorcontext.instance (). Resource ("SQL Mapper Configuration");  This. Configuration.setvariables (props);  This. parsed =false;  This. Environment =environment;  This. Parser =parser; }  //call this method to parse the MyBatis configuration file and return the Config object   PublicConfiguration Parse () {if(parsed) {Throw NewBuilderexception ("Each xmlconfigbuilder can is only used once."); } parsed=true; //from the root node configuration, start parsingParseconfiguration (Parser.evalnode ("/configuration")); returnconfiguration; }    //resolves 10 sub-nodes under a configuration node.   Private voidparseconfiguration (xnode root) {Try{propertieselement (Root.evalnode ("Properties"));//issue #117 Read Properties firstTypealiaseselement (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 #631Databaseidproviderelement (Root.evalnode ("Databaseidprovider")); Typehandlerelement (Root.evalnode ("Typehandlers")); Mapperelement (Root.evalnode ("Mappers")); } Catch(Exception e) {Throw NewBuilderexception ("Error parsing SQL Mapper Configuration. Cause: "+e, E); }  }  }  

From the above can be seen can be configured 10 sub-nodes, respectively: properties, typealiases, Plugins, Objectfactory, objectwrapperfactory, settings, environments, Databaseidprovider, Typehandlers, mappers.

The use and principle analysis of mapper mapping configuration in MyBatis frame (ii) configuration Chapter Sqlsessionfactorybuilder,xmlconfigbuilder

Related Article

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.