Builder Patterns in the MyBatis Framework: XML ConfigBuilder and XML MapperBuilder
In the MyBatis framework, XML ConfigBuilder and XML MapperBuilder are typical applications of the builder pattern.
The Builder pattern is a creative design pattern used to separate the construction process of objects from their representations, so that different representations can be created from the same construction process. The builder pattern delegates the construction process of an object to different builders, each responsible for building a portion of the object, and finally the commander calls the builder's methods to assemble the object. This can simplify the process of creating complex objects and improve flexibility.
XML ConfigBuilder and XML MapperBuilder are two important builder classes in the MyBatis framework.
The XML ConfigBuilder is used to parse the configuration file of MyBatis, which is mybatis config. xml. Its main responsibility is to build the Configuration object. The XML ConfigBuilder delegates the parsing of various sub configurations (such as data source configuration, type alias configuration, mapper configuration, etc.) to the corresponding builders for processing based on the content of the configuration file, and finally integrates these configurations together through the Configuration object.
The XML Mapper Builder is used to parse Mapper mapping files, namely *. xml. Its main responsibility is to build the MappedStatement object and register it with the Configuration object. The XML Mapper Builder constructs a MappedStatement object by parsing SQL statements and corresponding parsers in the Mapper mapping file.
The following are some original codes for the XML ConfigBuilder and XML MapperBuilder in the MyBatis framework:
// XMLConfigBuilder.java
public class XMLConfigBuilder extends BaseBuilder {
public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
// ...
}
public Configuration parse() {
// ...
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
private void parseConfiguration(XNode root) {
try {
// ...
XNode environmentsNode = root.evalNode("environments");
parseEnvironments(environmentsNode);
// ...
} catch (Exception e) {
// ...
}
}
// ...
}
// XMLMapperBuilder.java
public class XMLMapperBuilder extends BaseBuilder {
public XMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource, Map<String, XNode> sqlFragments, String namespace) {
// ...
}
public void parse() {
try {
// ...
XNode context = document.getRootElement();
// ...
configurationElement(context);
} catch (Exception e) {
// ...
}
}
private void configurationElement(XNode context) {
try {
String namespace = context.getStringAttribute("namespace");
// ...
BuildStatementFromContext (context. evalNodes ("select | insert | update | delete"));
// ...
} catch (Exception e) {
// ...
}
}
private void buildStatementFromContext(List<XNode> list) {
// ...
for (XNode context : list) {
final XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, context, requiredDatabaseId);
// ...
statementParser.parseStatementNode();
}
}
// ...
}
Summary:
Through the builder pattern, the MyBatis framework separates the construction process of objects from their representation, making the construction process flexible and easy to maintain. The XML ConfigBuilder and XML MapperBuilder are used to parse configuration and mapping files, build them into corresponding objects, and register them in the Configuration object of MyBatis. This can achieve unified management and flexible use of configuration files and mapping files. The application of builder pattern in MyBatis framework reflects the Open–closed principle and single responsibility principle in Object-oriented design principles.