Method of verifying sqlmapper configuration file of mybatis using XSD (1)
Based on the refactoring of sqlsessionfactorybean, this article briefly reviews the operations:
Create a new sqlsessionfactorybean with the same initial code as mybatis spring;
Reconstruct the buildsqlsessionfactory () method, extract many if statements into a group of small methods, reserve custom configuration methods, and add getter methods of common attributes;
Extract the component factory interface and provide the component creation tool class sqlsessioncomponetfactories to centrally manage new xxx() scattered in different places for component replacement.
Now let's see how to extend. First, create a schemasqlsessionfactorybean, inherit the reconstructed sqlsessionfactorybean, and synchronously modify it to a new class in the XML configuration:
For some simple function extensions, such as setting the default result type and scanning the abbreviation of the specified type, there is no more discussion here. Here we focus on how to extend it to use XSD to verify the sqlmapper configuration.
1、 Override the doparseqlmaperresource () method in the sqlsessionfactorybean. This method is used to parse an sqlmapper configuration file
Of course, for compatibility, you need to first judge whether it is a DTD. If it is a DTD, resolve it according to the original method, otherwise resolve it according to the user-defined method:
Here, the xmlvalidationmodedetector in spring is used to detect the verification mode of XML configuration files. It is also very simple in logic. It is read line by line. Before the beginning of the text, if DTD definition is found, DTD mode will be returned, otherwise XSD mode will be returned (in fact, spring is not only used for the detection mode, but also used for the subsequent custom namespace).
So far, the parsing of sqlmapper configuration file has been divided into two branches, which are compatible with the official parsing of mybatis, and navigate the parsing in XSD mode to the method doparseqlperresourcewithschema().
2、 Write an XSD file for verifying sqlmapper (you need to have some basic knowledge of XSD. Please refer to the learning notes on XML in this blog)
1. First, use an XML tool to convert mybatis's DTD file into the original XSD file. Many XML tools have this function and can be searched online
There are three levels:
(1) Root element (mapper element): corresponding to an sqlmapper file, it has a namespace attribute that represents a logical classification of its child elements. It should be noted that the namespace attribute here is different from the XML namespace. The former is a logical classification of mybatis itself, and the latter is used to define XML elements and attribute constraints that can appear in XML files.
(2) First level sub element (CACHE | cache ref | resultmap | parametermap | SQL | insert | update | delete | select): the first level sub element of mapper. Because the mybatis framework handles the first level sub elements differently, it is used as a separate level here, because it mainly adds, deletes and changes query statements, so it is called statement level statement element
(3) Other elements (SQL configuration text, include | trim | where | set | foreach | choose | if): text used to configure SQL script and dynamic script elements, which are called script level script elements
2. Make the following modifications on the basis of generating XSD files
(1) Add a namespace, such as:
(2) Wrap the first level element into an element group statementgroup
(3) Modify the mapper element to allow elements with other namespaces
(4) Wrap dynamic script elements into an element group dynascriptgroup and allow other named elements to appear
(5) Use dynascriptgroup to replace places where dynamic script elements appear, such as < Select > elements
(6) For other optimizations, for example, the three values statementtype can take, statement, prepared and callable, are defined as enumeration types:
Similarly, there are parametermode, JDBC type, javatype, and so on.
The above is a summary of the sqlmapper configuration file of mybatis verified by XSD introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!