Mybatis MetaObject和ObjectWrapper设计概念
Posted BINGJJFLY
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis MetaObject和ObjectWrapper设计概念相关的知识,希望对你有一定的参考价值。
ObjectWrapper
该类的作用是对象的包装,对对象属性的操作,其行为有
BaseWrapper
具备了MetaObject,对象包装的公共父类,主要处理集合类型
BeanWrapper
具备了储存对象的Object和MetaClass
@Override public Object get(PropertyTokenizer prop) { if (prop.getIndex() != null) { // 使用MetaObject逐级实例化集合对象 Object collection = resolveCollection(prop, object); // 使用MetaObject获得Value return getCollectionValue(prop, collection); } else { return getBeanProperty(prop, object); } }
MetaObject
private final Object originalObject; private final ObjectWrapper objectWrapper; private final ObjectFactory objectFactory; private final ObjectWrapperFactory objectWrapperFactory; private final ReflectorFactory reflectorFactory; private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) { this.originalObject = object; this.objectFactory = objectFactory; this.objectWrapperFactory = objectWrapperFactory; this.reflectorFactory = reflectorFactory; // 根据originalObject的类型实例化具体的ObjectMapper,将MetaObject自己注入进去,委派模式 if (object instanceof ObjectWrapper) { this.objectWrapper = (ObjectWrapper) object; } else if (objectWrapperFactory.hasWrapperFor(object)) { this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object); } else if (object instanceof Map) { this.objectWrapper = new MapWrapper(this, (Map) object); } else if (object instanceof Collection) { this.objectWrapper = new CollectionWrapper(this, (Collection) object); } else { this.objectWrapper = new BeanWrapper(this, object); } }
获得初始对象的属性值通过ObjectWrapper并可能委派给MetaObject来获得。
以上是关于Mybatis MetaObject和ObjectWrapper设计概念的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis 源码学习-反射工具(ObjectWrapper & MetaObject)
Mybatis 源码学习-反射工具(ObjectWrapper & MetaObject)
Mybatis常用工具类-- MetaObjectMetaClass