Mybatis source code parsing - initialization

Profiles

Mybatis initialization mainly parsing of configuration files, mainly Mybaits-config.xml and * Mapper.xml resolution

Builder mode

Mybatis use the builder pattern to build parsing the configuration file

BaseBuilder

Abstract class acts as an interface builder, which is a subclass of concrete builder, responsible for different analytical tasks

XMLConfigBuilder

Mybaits-config.xml file is responsible for parsing
parse () method is the core, it returns a Configuration object, which contains all the configuration of the content Mybatis



private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

Can be seen from the process, each node of the main configuration file and parses the provisioning content is added to the global configuration object Configuration

XMLMapperBuilder

Responsible for parsing the xml mapping file


private void configurationElement(XNode context) {
    try {
      String namespace = context.getStringAttribute("namespace");
      if (namespace == null || 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. The XML location is '" + resource + "'. Cause: " + e, e);
    }
  }

As shown namespace code will map file, cache, cache-ref, parameterMap, resultMap, sql statement node and resolution.

cache

Used in the secondary cache, using MapperBuilderAssistant.useNewCache () Cache object created, and add it to Configuration.caches (StrictMap type, Throws insert duplicate key) stored, as a key id cache (default namespace mapping file)

Cache-ref

If you want to share a namespace plurality of secondary cache, i.e. a cacha share the same object using the node.

<cache-ref namespace="namespace1">

Object shared cache on behalf of namespace1

ResultMap

ResultMap ResultMap node corresponding object in each node corresponds to a resultMap ResultMapping object.
ResultMap following properties:

  private Configuration configuration;
  //resultMap的id
  private String id;
  //resultMap对应的Type
  private Class<?> type;
  //所有的resultMappings对象集合
  private List<ResultMapping> resultMappings;
  //带有id标志的映射关系,如<id>和<idArg>
  private List<ResultMapping> idResultMappings;
  //<constructor>标志的所有映射关系,如<constructor>节点下的所有子元素
  private List<ResultMapping> constructorResultMappings;
  //除了<constructor>标志之外的所有映射关系
  private List<ResultMapping> propertyResultMappings;
  //映射关系中涉及的column名字
  private Set<String> mappedColumns;
  //映射关系中涉及的所有属性名字
  private Set<String> mappedProperties;
  //鉴别器
  private Discriminator discriminator;
  //是否有嵌套结果映射
  private boolean hasNestedResultMaps;
  //是否有嵌套查询
  private boolean hasNestedQueries;
  //是否开启自动映射
  private Boolean autoMapping;

ResultMapping following properties:

  private Configuration configuration;
  // 列名
  private String property;
  // 属性名
  private String column;
  private Class<?> javaType;
  private JdbcType jdbcType;
  private TypeHandler<?> typeHandler;
  private String nestedResultMapId;
  private String nestedQueryId;
  private Set<String> notNullColumns;
  private String columnPrefix;
  private List<ResultFlag> flags;
  private List<ResultMapping> composites;
  private String resultSet;
  private String foreignColumn;
  private boolean lazy;

Most see the name EENOW

<result id="BaseResult" type = "cn.jwb5.test.User">
	<id column = "id" property = "id"/>
	<id column = "user_name" property = "userName"/>
	<id column = "pwd" property = "pwd"/>
	<id column = "email" property = "email"/>
</result>

Map shown in FIG.
Here Insert Picture Description

resultMap objects will be added to the collection after Configuration.resultMaps building, other similar <constructor>、<assocation>、<collection>peer nodes can actually be seen as a nested ResultMap

XMLStatementBuilder

For parsing SQL node, including for <include> 和 <selectKey>the resolution, the rest of dynamic tag to SqlNode and resolve SqlSource

include

The <include>replacement node to <sql>sql fragment corresponding node


<sql id="somesql">
        from ${tablename}
</sql>
    
<select id="find" resultType="int">
    select * from FROM 
    <include refid="somesql">
        <property name="somesql" value="user"/>
    </include>
</select>

Shown in the timing diagram of FIG.

Here Insert Picture Description

selectKey

parseSelectKeyNode()Will by XMLLanguageDriver.createSqlSource()creating the corresponding SqlSourceobjects, using the interior XMLScriptBuilder.parseScriptNode()to create a corresponding SqlSourceobject to resolve the specific analytical methods discussed before SeeSqlSource&SqlNode

Published 98 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/Mutou_ren/article/details/102813241