Mybatis 源码解析三、Mapper接口与mapper.xml文件绑定

本文深入探讨Mybatis中Mapper接口如何与对应的mapper.xml文件进行绑定,解析这一关键机制,涉及动态代理、XML配置解析等核心概念。
摘要由CSDN通过智能技术生成
     一、流程图介绍整体过程
        
 
     
    1、首先根据MapperScannerConfigurer进行包扫描,扫描Mapper接口,生成Spring特定的描述,并将其交由MapperProxyFactory管理,后期会由其生成动态代理对象。
              ClassOathMapperScanner 中的doScan()方法。
                
     
     
     
  1. GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
  2. if (logger.isDebugEnabled()) {
  3. logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName()
  4. + "' and '" + definition.getBeanClassName() + "' mapperInterface");
  5. }
  6. // the mapper interface is the original class of the bean
  7. // but, the actual class of the bean is MapperFactoryBean
  8. definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());
  9. definition.setBeanClass(MapperFactoryBean.class);
  10. definition.getPropertyValues().add("addToConfig", this.addToConfig);


    2、初始化SqlSessionFactoryBean。首先判定mybatis.xml中的mapper属性是否配置如果配置,解析Mapper接口添加到Configuration中。
            XMLConfigBuilder中的mapperElement()方法
            
     
     
     
  1. private void mapperElement(XNode parent) throws Exception {
  2. if (parent != null) {
  3. for (XNode child : parent.getChildren()) {
  4. if ("package".equals(child.getName())) {
  5. String mapperPackage = child.getStringAttribute("name");
  6. configuration.addMappers(mapperPackage);
  7. } else {
  8. String resource = child.getStringAttribute("resource");
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值