MyBatis Source Text---XML configuration of the Mappers mapper

Source: Internet
Author: User

Here I simply tell you how to parse the XML configuration file and finish loading when creating the Sqlsessionfactory object.

The mapperelement is called to parse the mappers when the configuration node and child nodes are parsed from the MyBatis profile.

      /**
       * Parse mappers node and node *
       /
      mapperelement (Root.evalnode ("mappers"));


  /** * Parse XML configuration file mappers node * @param parent * @throws Exception */private void Mapperelement (xnode parent) t Hrows Exception {if (parent! = null) {for (XNode Child:parent.getChildren ()) {/** * Mappe
         Under the RS node, configure our mapper mapping file, the so-called mapper mapping file, which is a bridge for MyBatis to build data tables and javabean mappings. * In our actual development, usually a mapper file corresponds to a DAO interface, this mapper can be seen as a DAO implementation.
         Therefore, the mappers must be configured.
         * For mapper configuration, the same as typealiases, there are two ways *<mappers> * <mapper resource= "Demo1/usermapper.xml"/> * <mapper class= "Hzq.mybatis.source.demo1.dao.IUserDao"/> * <mapper url= "File:///var/demo1/Use Rmapper.xml "/> * <package name=" Hzq.mybatis.source.demo1.dao "/> </mappers> * ma  Pper and package two ways, but for mapper configuration, there are three different references, see the source * package is loaded with all the interfaces of the class file "Register all interfaces
 As Mappers ", required with <mapper class=" Hzq.mybatis.source.demo1.dao.IUserDao "/> This configuration Way * Mapper:        * Resource---"Using classpath relative resources" Use relative path * URL----"Using URL fully qualified Pat The HS "uses the full path * class--" Using Mapper interface Classes "To use the interface class of the mapper file, the interface is specified through class, and the interface is mapped to the corresponding XML file, but must be  Ensure that the interface and mapper file have the same name (case insensitive) * */if ("Package". Equals (Child.getname ())) {/** *
          Gets the package name of the MyBatis configuration file */String Mapperpackage = Child.getstringattribute ("name"); 
        /** * Parse the Mapper file under the package directory and load it into the configuration */Configuration.addmappers (mapperpackage);
          } else {String resource = Child.getstringattribute ("resource");
          String url = child.getstringattribute ("url");
          String Mapperclass = Child.getstringattribute ("class"); /** * Mapper configuration mode, Class,resource, URL 3 any one */if (resource! = null && URL = = nul L && Mapperclass = = null) {//Resource configuration mode
            Errorcontext.instance (). resource (Resource); /** * Read Mapper file */InputStream InputStream = Resources.getresourceasstream (Resource
            ); /** * Mapper mapping files are parsed through Xmlmapperbuilder */xmlmapperbuilder mapperparser = new XMLMap
            Perbuilder (inputstream, configuration, Resource, configuration.getsqlfragments ());
          Mapperparser.parse (); } else if (resource = = NULL && URL! = NULL && Mapperclass = null) {//url configuration by Er
            Rorcontext.instance (). resource (URL);
            InputStream inputstream = resources.geturlasstream (URL); Xmlmapperbuilder mapperparser = new Xmlmapperbuilder (inputstream, configuration, URL, configuration.getsqlfragments ()
            );
          Mapperparser.parse ();
            } else if (resource = = NULL && URL = = NULL && Mapperclass! = null) {//class configuration method Class<?> MappErinterface = Resources.classforname (Mapperclass);
          Configuration.addmapper (Mapperinterface); } else {throw new Builderexception ("A mapper element may be only specify a URL, resource or class, and not more
 than one. ");}}}}

For the details of how the mapping file with Mpper is parsed, I will show you the detailed flow of the elements in the Mpper map.




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.