Mybatis source code analysis: BaseBuilder

BaseBuilder source code analysis

BaseBuilder is mybatis profile, base Mapper files, parser class, which is an abstract class, but not related abstract method declarations, all subclasses and attribute field may be used to achieve methods BaseBuilder provided, at each BaseBuilder when using subclass constructor instantiated BaseBuilder bound to call the constructor, this is because the internal BaseBuilder maintains three attributes configuration, TypeAliasRegistry, TypeHandlerRegistry. Configuration itself is a "hodgepodge", maintains a wide variety of internal object instance. TypeAliasRegistry, TypeHandlerRegistry is one of the two, in fact, these two examples BaseBuilder is obtained from the Configuration, why do it? When writing mybatis profile, and sometimes need to customize the parameters of the alias and type of processor, parsing process configuration file must be customized alias processor back into the configuration.

Properties and constructor snippet

   // configuration class 
  protected  Final the Configuration Configuration;
   // type alias register is 
  protected  Final TypeAliasRegistry typeAliasRegistry;
   // type processor register is 
  protected  Final TypeHandlerRegistry typeHandlerRegistry; 

  public BaseBuilder (the Configuration Configuration) {
     the this .configuration = Configuration;
     // Get type alias registrar 
    the this .typeAliasRegistry = the this .configuration.getTypeAliasRegistry ();
     // Get the type of the registrar processing 
    the this .typeHandlerRegistry = the this .configuration.getTypeHandlerRegistry (); 
  }

 

BaseBuilder provides a more basic method, here are some basic methods (code is too simple, omitted) and a more important way.

Method name parameter name effect
parseExpression regex,defaultValue Get regular expression object, if the regex expression is null, the default value defaultValue as the expression
booleanValueOf value,defaultValue Gets a boolean value, and if it is null, the default value defaultValue
integerValueOf value,defaultValue Gets shaping value value, if null, the default value defaultValue
stringSetValueOf value,defaultValue Get value of value, according to the ',' and is divided into an array HashSet, if the value is null, then the default value defaultValue
resolveJdbcType alias The JDBC query data corresponding to the type of alias, is mybatis jdbcType java.sql.Types of primary packaging, and is an enumeration class, detailed information can be viewed org.apache.ibatis.type.JdbcType
resolveResultSetType alias Alias obtain a corresponding result set according to the detailed information reference org.apache.ibatis.mapping.ResultSetType. This class is java.sql.ResultSet packages, java.sql.ResultSet provides three values.
ResultSet.TYPE_FORWORD_ONLY cursor result set can only scroll down.
ResultSet.TYPE_SCROLL_INSENSITIVE cursor result set can be moved up and down, when the database changes, the current result set unchanged.
ResultSet.TYPE_SCROLL_SENSITIVE scrollable return result sets, when the database changes, the current result set sync change.
resolveParameterMode alias The alias acquisition ParameterMode Optional value IN, OUT, INOUT, more information can be referred org.apache.ibatis.mapping.ParameterMode class

There are two important BaseBuilder method createInstance (String alias) according to the alias object instance , resolveTypeHandler (Class <?> JavaType , String typeHandlerAlias) parsed according to the type of processor and the alias java class type of processor , the first look createInstance method.

createInstance method : create an alias for the instance of the object based on, if alias is null, then return null object directly, or according to class is instantiated alias found in the following code, according to the call chain is found under typeAliasRegistry BaseBuilder call resolveAlias ()method.
I said before typeAliasRegistry is taken directly from the Configuration object, then Configuration initializes a direct TypeAliasRegistry objects, find org.apache.ibatis.type.TypeAliasRegistry constructor can be found registered a large number of basic data types, and then look typeAliasRegistry .resolveAlias () method. Firstly TYPE_ALIASES find the corresponding class according to the alias alias registered a set that does not exist, the Resources.classForName (string) to obtain the corresponding class, now only need to know the effect of Resources.classForName with Class.forname () the effect is the same, for the return path according to the class to the class object. Follow-up analyzes Resources object.

 1   protected Object createInstance(String alias) {
 2     Class<?> clazz = resolveClass(alias);
 3     if (clazz == null) {
 4       return null;
 5     }
 6     try {
 7       return resolveClass(alias).newInstance();
 8     } catch (Exception e) {
 9       throw new BuilderException("Error creating instance. Cause: " + e, e);
10     }
11   }
12 
13   protected Class<?> resolveClass(String alias) {
14     if (alias == null) {
15       return null;
16     }
17     try {
18       return resolveAlias(alias);
19     } catch (Exception e) {
20       throw new BuilderException("Error resolving class. Cause: " + e, e);
21     }
22   }
23   protected Class<?> resolveAlias(String alias) {
24         return typeAliasRegistry.resolveAlias(alias);
25       }

 

resolveAlias ​​() method

 1  public <T> Class<T> resolveAlias(String string) {
 2     try {
 3       if (string == null) {
 4         return null;
 5       }
 6       // issue #748
 7       String key = string.toLowerCase(Locale.ENGLISH);
 8       Class<T> value;
 9       if (TYPE_ALIASES.containsKey(key)) {
10         value = (Class<T>) TYPE_ALIASES.get(key);
11       } else {
12         value = (Class<T>) Resources.classForName(string);
13       }
14       return value;
15     } catch (ClassNotFoundException e) {
16       throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);
17     }
18   }

 

resolveTypeHandler: used to create an alias type corresponding to the type of processor, the processor with an alias Similarly, first determines whether or null according to the type of alias, the alias register is again find the alias corresponding to the class type, class type used in the registration process type find an instance of the class corresponding to the type, if the instance does not exist, the reflective instantiate it. The following is a process analysis to create the type of processor. We can see, no matter what type you want to use an alias, must be <alias, class type> Register to obtain correspondence between the alias registered vessel. If the type is a class, the processing needed in the alias register is a register <class type, processor instance> to the type of mapping relationship.

  • The processor determines whether the alias is null type, if the value is null, then return null object directly, or to find the class type is already registered in the alias register vessel
  • If the class type found is not null and does not belong to TypeHandler type, an exception is thrown BuilderException
  • Looking to the class type strongly into TypeHandler type, the type of processor instance to find the registered type according to the class type processor
  • If the processor can not find the type of the corresponding instance, the reflective instantiated.

 

Reference documents

Guess you like

Origin www.cnblogs.com/zhengzuozhanglina/p/11291553.html