MyBatis的SQL解析也是一個很是重要的一部分。對應的類XMLMapperBuilder,在下面咱們只是列出相應的方法,具體分析在後面文章介紹:
java
1 屬性sql
/** 解析XML文檔轉換成DOM對象 */ private XPathParser parser; private MapperBuilderAssistant builderAssistant; private Map<String, XNode> sqlFragments; private String resource;
繼承了MyBatis整合Spring的實現(2)中1.2父類XMLConfigBuilder的屬性。其它屬性具體的功能能夠後來補充。
緩存
2 XMLMapperBuilder實例化:app
public XMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource, Map<String, XNode> sqlFragments) { this(new XPathParser(inputStream, true, configuration.getVariables(), new XMLMapperEntityResolver()), configuration, resource, sqlFragments); } private XMLMapperBuilder(XPathParser parser, Configuration configuration, String resource, Map<String, XNode> sqlFragments) { super(configuration); this.builderAssistant = new MapperBuilderAssistant(configuration, resource); this.parser = parser; this.sqlFragments = sqlFragments; this.resource = resource; }
這裏只是對屬性的賦值,還有建立了MapperBuilderAssistant對象。ui
3 MapperBuilderAssistant
this
3.1 屬性spa
/** 當前命名空間 */ private String currentNamespace; /** 資源 */ private String resource; /** 當前緩存 */ private Cache currentCache; private boolean unresolvedCacheRef;
此類也繼承了MyBatis整合Spring的實現(2)中1.2父類XMLConfigBuilder的屬性。
.net
上圖就是此對象中的一個方法,原來此類的功能主要是獲取對應的Mapping的,這裏獲取了ResultMapping,還有ParameterMapping等等,這裏後面再具體分析。
code
4 具體解析代碼:對象
public void parse() { if (!configuration.isResourceLoaded(resource)) { configurationElement(parser.evalNode("/mapper")); configuration.addLoadedResource(resource); bindMapperForNamespace(); } parsePendingResultMaps(); parsePendingChacheRefs(); parsePendingStatements(); } private void configurationElement(XNode context) { try { String namespace = context.getStringAttribute("namespace"); if (namespace.equals("")) { throw new BuilderException("Mapper's namespace cannot be empty"); } builderAssistant.setCurrentNamespace(namespace); cacheRefElement(context.evalNode("cache-ref")); cacheElement(context.evalNode("cache")); parameterMapElement(context.evalNodes("/mapper/parameterMap")); resultMapElements(context.evalNodes("/mapper/resultMap")); sqlElement(context.evalNodes("/mapper/sql")); buildStatementFromContext(context.evalNodes("select|insert|update|delete")); } catch (Exception e) { throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e); } }
代碼就分解到這個程度,下面的文章對代碼中每一個方法進行分析。