From 21c5ee3e37c696d19d0e441e9c7c263f3a2cd4bd Mon Sep 17 00:00:00 2001 From: huifer Date: Mon, 30 Dec 2019 08:52:11 +0800 Subject: [PATCH] mybatis metaObject --- .../核心处理层/Mybatis-MetaObject.md | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 docs/Mybatis/核心处理层/Mybatis-MetaObject.md diff --git a/docs/Mybatis/核心处理层/Mybatis-MetaObject.md b/docs/Mybatis/核心处理层/Mybatis-MetaObject.md new file mode 100644 index 0000000..9d66f98 --- /dev/null +++ b/docs/Mybatis/核心处理层/Mybatis-MetaObject.md @@ -0,0 +1,111 @@ +# Mybatis MetaObject +- Author: [HuiFer](https://github.com/huifer) +- 源码位于:`org.apache.ibatis.reflection.MetaObject` +```java +/** + * @author Clinton Begin + */ +public class MetaObject { + + /** + * 原始的数据对象,初始化时的对象 + */ + private final Object originalObject; + /** + * 对象包装 + */ + private final ObjectWrapper objectWrapper; + /** + * object 工厂 + */ + private final ObjectFactory objectFactory; + /** + * object + */ + 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; + + // 根据object不同实例进行不同的实例化方式 + 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); + } + } + + public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) { + if (object == null) { + return SystemMetaObject.NULL_META_OBJECT; + } else { + return new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory); + } + } + + /** + * 获取value + * @param name 属性值名称 + * @return + */ + public Object getValue(String name) { + PropertyTokenizer prop = new PropertyTokenizer(name); + if (prop.hasNext()) { + MetaObject metaValue = metaObjectForProperty(prop.getIndexedName()); + if (metaValue == SystemMetaObject.NULL_META_OBJECT) { + // 判断是否是空的metaObject + return null; + } else { + return metaValue.getValue(prop.getChildren()); + } + } else { + return objectWrapper.get(prop); + } + } + + /** + * metaObject 设置属性值方法 + * {name:value} + * + * @param name 属性值名称 + * @param value 属性值 + */ + public void setValue(String name, Object value) { + PropertyTokenizer prop = new PropertyTokenizer(name); + if (prop.hasNext()) { + // 获取属性实例 + MetaObject metaValue = metaObjectForProperty(prop.getIndexedName()); + if (metaValue == SystemMetaObject.NULL_META_OBJECT) { + if (value == null) { + // value 空则返回 + // don't instantiate child path if value is null + return; + } else { + // 创建属性值 + metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory); + } + } + + metaValue.setValue(prop.getChildren(), value); + } else { + objectWrapper.set(prop, value); + } + } + + +} + +``` \ No newline at end of file