mybatis succinctly (four) - ObjectFactory

Foreword

  • ObjectFactory What some people may not know. It does not matter Today we look at the role of this class. First, he is used to create mybatis returned results set. Through which we can create a control node returns set.

mybatis的ObjectFactory

  • This class is obtained by mybatis-config.xml configured. Provides a objectFactorylabel for our configuration. In Configurationthe following the code

protected ObjectFactory objectFactory = new DefaultObjectFactory();
  • The above code can understand. The results indicate mybatis the default is to create a plantDefaultObjectFactory

public interface ObjectFactory {

  /**
   * 设置一些额外的属性 通过在mybatis-config.xml中objectFactory中properties属性标签设置 , 在初始化mybatis是就会触发这个方法
   */
  void setProperties(Properties properties);

  /**
   * 已默认的构造函数(无参构造)实例化对象
   * @return
   */
  <T> T create(Class<T> type);

  /**
   * 指定的构造函数实例化对象
   * @param type Object type
   * @param constructorArgTypes Constructor argument types
   * @param constructorArgs Constructor argument values
   * @return
   */
  <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs);

  /**
   * 判断是否是集合类型。只要能存储其他元素的都叫做集合
   * 内部通过isAssignableFrom判断是否继承值Collection类
   * @param type Object type
   * @return whether it is a collection or not
   * @since 3.1.0
   */
  <T> boolean isCollection(Class<T> type);

}
  • ObjectFactory action is used to create objects. We normally do not need to extend him. Let us look at his source

Source

setProperties

  • When loading Mybatis will load additional property configuration. Here in the above method annotated said has been very clear.

 create

  • objectFactory method create are actually working instantiateClass.
private  <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs);

instantiateClass

  • First, if there is no construction parameters and types of words. By constructor = type.getDeclaredConstructor();acquiring the constructor. Examples of objects and then through the secondary builder. There are likely to be privatized, mybatis do remedy is added to obtain proprietary rights being given time.

  • If not empty, then in accordance with the need to specify the type of configuration parameters to construct


constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
  • This code is to obtain the above specified type constructor. Then get the constructor then instantiated according to the corresponding parameters.

  • To sum it up is to construct an alternative in line with the instantiated objects.

scenes to be used

  • ObjectFactory this class the importance of self-evident, but we need to customize the scene not many. Because he is a single function, it is an instance of an object.
  • Have to do some extra work after only a few cases, we need to generate a control object when we need to override the factory.
  • For example, in the school system, our students in addition to basic information, we would like to add a temporary result of a recent property used to indicate total score. This time we can customize ObjectFactory to achieve. Fill in to query data After you create your object.
    Join team

# Join team

Micro-channel public number

Micro-channel public number

Guess you like

Origin www.cnblogs.com/zhangxinhua/p/11968765.html