Mybatis源码学习六(MetaObject)

MetaObject基本架构

MetaObject获取属性值流程

源码分析MetaObject获取属性值

public class MetaObjectTest {

    @Test
    public void test1(){
        Object blog = new Blog();
       Configuration configuration = new Configuration();
        MetaObject metaObject = configuration.newMetaObject(blog);
        User user = new User();
        user.setName("xxppp");

        ArrayList<Comment> comments = new ArrayList<>();
        Comment comment = new Comment();
        comment.setUser(user);
        comments.add(comment);
        metaObject.setValue("comments",comments);
       metaObject.getValue("comments[0].user.name");
    }
}

 经过属性分词器解析

 此时hasNext()为true表示存在子属性

调用getValue,此时参数和上一次已经不一样了。为comments[0],此时没有子属性,直接调用get方法获取comments[0]的值。

 再将获取到的comments[0]值包装为MetaObject。

继续调用getValue(),此时参数为user.name。

 此时是有子属性的,为name,会进入metaObjectForProperty()。

此时user没有子属性,调用get方法获取user值

 将获取到的user值包装为MetaObject。

继续调用getValue(),此时参数是name,没有子属性,直接调用get()获取name值

对BeanWrapper【get】方法单独分析

再次出现调用getValue() 

 

 根据属性名,反射执行getXxx方法

总结:获取属性主要是利用MetaObject中的属性分词器,和反射原理

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值