Sqlmapper configuration file with XSD checksum mybatis (1)

Source: Internet
Author: User

This article is based on the refactoring of Sqlsessionfactorybean and briefly reviews what it does:

    1. New Sqlsessionfactorybean, the initial code and mybatis-spring are the same;
    2. Reconstruct the Buildsqlsessionfactory () method, and extract many if statements as a set of small methods, reserving the method of custom configuration, and adding getter method of common attributes;
    3. Extract the component factory interface and provide the component creation tool class Sqlsessioncomponetfactorys, which will be scattered in different places in the new Xxx () centrally managed to facilitate the replacement of components.

Now to see how to extend, first create the Schemasqlsessionfactorybean, inherit the refactored Sqlsessionfactorybean, and modify the new class in the XML configuration synchronously:

Schemasqlsessionfactorybean extends sqlsessionfactorybean {}  

For some simple feature extensions, such as setting the default result type and scanning for the specified type abbreviation, there is no more discussion here about how to extend the configuration to use the XSD checksum sqlmapper.

First, covering The Sqlsessionfactorybean in the doparsesqlmapperresource () method, the function of this method is to parse a sqlmapper configuration file

Of course, for compatibility, you need to first determine whether it is a DTD, if it is a DTD, according to the original method of parsing, otherwise by custom method parsing:

 PackageOrg.dysd.dao.mybatis.schema;ImportOrg.apache.ibatis.executor.ErrorContext;Importorg.apache.ibatis.session.Configuration;Importorg.springframework.core.NestedIOException;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.util.xml.XmlValidationModeDetector; Public classSchemasqlsessionfactorybeanextendsSqlsessionfactorybean {@Overrideprotected voiddoparsesqlmapperresource (Configuration configuration, Resource mapperlocation)throwsnestedioexception {intmode =Detectvalidationmode (mapperlocation); if(mode = = XMLVALIDATIONMODEDETECTOR.VALIDATION_DTD) {//if it's a DTD, use the MyBatis official parsing            Super. Doparsesqlmapperresource (configuration, mapperlocation); }Else{            Try {                //using schema validation                 This. Doparsesqlmapperresourcewithschema (configuration, mapperlocation); } Catch(Exception e) {Throw NewNestedioexception ("Failed to parse Mapping resource: '" + mapperlocation + "'", E); } finally{errorcontext.instance (). reset (); }        }    }        protected voidDoparsesqlmapperresourcewithschema (Configuration configuration, Resource mapperlocation) {}Private intDetectvalidationmode (Resource mapperlocation)throwsnestedioexception {intmode =-1; Try{Xmlvalidationmodedetector Detector=NewXmlvalidationmodedetector (); Mode=Detector.detectvalidationmode (Mapperlocation.getinputstream ()); } Catch(Exception e) {Throw NewNestedioexception ("Failed to parse Mapping resource: '" + mapperlocation + "'", E); } finally{errorcontext.instance (). reset (); }        returnmode; }}
View Code

The Xmlvalidationmodedetector in spring is borrowed to detect the checksum mode of the XML configuration file, which is logically simple, that is, a line-by-line reading, before the body begins, it is found that there is a DTD definition, return to the DTD mode, or return XSD mode (in fact, Not only is the reconnaissance mode borrowed from spring, but the later custom namespaces also draw on spring.

At this point, the resolution of the Sqlmapper configuration file has been divided into two, compatible with the MyBatis official parsing, and the XSD mode of parsing navigation to the method Doparsesqlmapperresourcewithschema ().

Second, write the XSD file for verifying sqlmapper (need to have a certain XSD basic knowledge, can refer to this blog about the XML part of the Learning notes)

1, first use an XML tool, the MyBatis DTD file into the original XSD file, there are many XML tools have this function, you can search the Internet

This is divided into three tiers:

(1) root element (mapper Element): corresponding to a sqlmapper file, there is a namespace attribute that represents a logical classification of its child elements. Note that the namespace property here differs from the XML namespace, which is a logical classification of the MyBatis itself, which is used to define XML element and attribute constraints that can occur in an XML file.

(2) Primary sub-element (Cache|cache-ref|resultmap|parametermap|sql|insert|update|delete|select): The first-level sub-element of Mapper, Because the MyBatis framework has different processing for the first-level sub-elements, it is here as a single level, because the main is to delete and change the statement, so called the statement-level statement element

(3) Other elements (SQL configuration text, include|trim|where|set|foreach|choose|if): Text for configuring SQL scripts, and dynamic scripting elements, called Script-level scripting elements

2. Make the following modifications on the basis of generating the XSD file

(1) Add namespaces, such as:

<? XML version= "1.0" encoding= "UTF-8" standalone= "no" ?> <  xmlns= "Http://dysd.org/schema/sqlmapper"    targetnamespace= "http// Dysd.org/schema/sqlmapper "    xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "      elementFormDefault = "qualified" version = "1.0" >

(2) Packaging a first-level element as an element group Statementgroup

(3) Modifying the mapper element, allowing elements of other namespaces to appear

(4) Wrapping a dynamic script element as an element group Dynascriptgroup and allowing other named elements to appear

(5) Use Dynascriptgroup to replace the place where the dynamic script element appears, such as the <select> element

(6) Some other optimizations, such as three values statement, PREPARED, and callable that can be statementtype, are defined as enumeration types:

<Xsd:simpletypename= "StatementType">        <xsd:restrictionBase= "Xsd:token">            <xsd:enumerationvalue= "STATEMENT" />            <xsd:enumerationvalue= "PREPARED" />            <xsd:enumerationvalue= "callable" />        </xsd:restriction></Xsd:simpletype>

Similar to Parametermode, Jdbctype, Javatype and so on.

Sqlmapper configuration file with XSD checksum mybatis (1)

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.