细说Mybatis一级缓存、二级缓存以及mybatis获取mapper的面向接口编程思想(Mapper接口动态代理实现原理)(二)

上一章和大家分享了Mybatis一级缓存和二级缓存,本章将继续和大家分享Mapper接口动态代理实现原理,按照国际惯例,先看源码,然后结合原理,写一个自己的小demo,从理论到实战,真正掌握面向接口编程的思想。

还是使用上一章的工程,这里就不占用篇幅了,可以访问下面的网址访问。
https://blog.csdn.net/CSDN_LICY/article/details/108764141

一、原理篇
(首先说明下我的idea快捷方式设置的是和eclipse一样,F5步进,F6下一步,F7步出,F8到下一断点。)
首先从Mybatis加载config.xml配置文件并解析mapper开始讲解。

  private void bindMapperForNamespace() {
    //获取namespace,xml里面的namespace一定要和mapper接口的全路径名一致
    String namespace = builderAssistant.getCurrentNamespace();
    if (namespace != null) {
      Class<?> boundType = null;
      try {
        //interface com.study.mybatisdemo.mapper.CountryMapper
        boundType = Resources.classForName(namespace);
      } catch (ClassNotFoundException e) {
        //ignore, bound type is not required
      }
      if (boundType != null) {
        if (!configuration.hasMapper(boundType)) {
          // Spring may not know the real resource name so we set a flag
          // to prevent loading again this resource from the mapper interface
          // look at MapperAnnotationBuilder#loadXmlResource
          configuration.addLoadedResource("namespace:" + namespace);
          //将CountryMapper添加到configuration
          configuration.addMapper(boundType);
        }
      }
    }
  }

此时位于MapperRegistry的addMapper方法,

  public <T> void addMapper(Class<T> type) {
  	//必须是接口类型,因为使用的是jdk动态代理
    if (type.isInterface()) {
      //如果knownMappers已经存在了此类型的MapperProxyFactory,则抛出异常。
      if (hasMapper(type)) {
        throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
      }
      boolean loadCompleted = false;
      try {
      	//首先根据Type构建一个MapperProxyFactory,并按照type放到knownMappers
        knownMappers.put(type, new MapperProxyFactory<>(type));
        // It's important that the type is added before the parser is run
        // otherwise the binding may automatically be attempted by the
        // mapper parser. If the type is already known, it won't try.
        MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
        parser.parse();
        loadCompleted = true;
      } finally {
        if (!loadCompleted) {
          knownMappers.remove(type);
        }
      }
    }
  }

到此,已经将Country的工厂类放到了knownMappers,接下来看是如何获取的mapper
在这里插入图片描述
此时位于MapperRegistry的getMapper方法,可以看到从knownMappers取出来了mapperProxyFactory,然后利用mapperProxyFactory.newInstance获取一个实例,继续走,看newInstance怎么做的。
在这里插入图片描述
此时位于MapperProxyFactory的newInstance方法,将sqlSession传递进去构建一个MapperProxy。
在这里插入图片描述
此时此时位于MapperProxyFactory的newInstance方法,可以看到使用Proxy返回一个代理对象。
在这里插入图片描述
再次回到测试案例,发现此时countryMapper就是一个代理对象。那这个代理对象是怎么执行的呢?继续往下走。
在这里插入图片描述
MapperProxy实现了InvocationHandler,因此走到invoke方法,首先获取PlainMethodInvoker,然后调用PlainMethodInvoker的invoke方法。
在这里插入图片描述
PlainMethodInvoker的invoke方法如下:
在这里插入图片描述
最终来到MapperMethod的execute方法,可以看到此时已经将mapper方法转换成了sqlSession方法的调用。此时的command.getName()返回的是com.study.mybatisdemo.mapper.CountryMapper.selectById,就是XML文件中namespace和具体方法id的组合。
在这里插入图片描述
总结如下:当调用一个接口的方法时,会先通过接口的全限定名称和当前调用的方法名的组合得到一个方法id,这个id就是映射xml中namespace和具体方法id的组合。所以可以在代理方法中使用sqlsession以命名空间的方式调用方法。通过这种方式可以将接口和XML文件中的方法关联起来。这种代理方式和常规的不同之处在于,这里没有对某个具体类进行代理,而是通过代理转化成了对其他代码的调用。

哈哈,通过上面的原理讲解,大家是不是对动态代理更加感兴趣了,下面通过一个简单的例子,来实战一下。

二、实战篇
(1)首先准备一个接口,用于模仿mapper

public interface Sell {
    public void sellSomeThing(String kind);
    public void sellOnline();
}

(2)准备两个实现类,实现sell接口,这是常规的动态代理实现

public class SellApple implements Sell{

    public SellApple() {
        System.out.println("苹果商铺");
    }

    @Override
    public void sellSomeThing(String kind) {
        System.out.println("开始卖苹果,种类:" + kind + "=========");
    }

    @Override
    public void sellOnline() {

    }
}

public class SellJuzhi implements Sell{

    public SellJuzhi() {
        System.out.println("橘子商铺");
    }

    @Override
    public void sellSomeThing(String kind) {
        System.out.println("开始卖橘子,种类:" + kind + "=========");
    }

    @Override
    public void sellOnline() {

    }
}

(3)准备一个普通类,用于模仿sqlsession,通过动态代理这个桥梁将对接口方法的调用转换为对其他方法的调用

public class OnLineShopping {

    public String sellOnLine() {
        System.out.println("我是电子商铺,不需要店铺,哈哈哈");
        return "电子商铺";
    }

}

(4)准备一个普通的动态代理类增强器InvocationHandler

public class MyInvocationHandler implements InvocationHandler {
    private Sell sell;

    public MyInvocationHandler(Sell sell) {
        this.sell = sell;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开始卖东西喽。。。。。。。。。。。");
        Object invoke = method.invoke(sell, args);
        System.out.println("卖完了,好开心。。。。。");
        return invoke;
    }

}

(5)准备一个MybatisInvocationHandler 用于模仿mybatis的mapper调用流程

public class MybatisInvocationHandler implements InvocationHandler {
    private OnLineShopping onLineShopping;

    public MybatisInvocationHandler(OnLineShopping onLineShopping) {
        this.onLineShopping = onLineShopping;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //将sell接口中sellOnLine的方法,转换为onLineShopping中sellOnLine方法的调用
        return onLineShopping.sellOnLine();
    }

}

(6)主测试案例

public class SellMain {

    public static void main(String[] args) {
        //这是将代理类生成到本地,可以通过看代理类源码的方式,更加了解代理类是如何执行的。
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        //普通的代理类,是有实现类的
        Sell sellApple = (Sell) Proxy.newProxyInstance(SellApple.class.getClassLoader(), SellApple.class.getInterfaces(), new MyInvocationHandler(new SellApple()));
        Sell sellJuzhi = (Sell) Proxy.newProxyInstance(SellJuzhi.class.getClassLoader(), SellJuzhi.class.getInterfaces(), new MyInvocationHandler(new SellJuzhi()));
        sellApple.sellSomeThing("红富士");
        System.out.println("--------------------------------------------");
        sellJuzhi.sellSomeThing("贡菊");
        System.out.println("---------------------面向接口编程-----------------------");
        //将sell的sellOnline转换为OnLineShopping的sellOnline
        Sell sell = (Sell) Proxy.newProxyInstance(OnLineShopping.class.getClassLoader(), new Class[] { Sell.class }, new MybatisInvocationHandler(new OnLineShopping()));
        sell.sellOnline();
    }

}

结果如下:
在这里插入图片描述
至此细说Mybatis一级缓存、二级缓存以及mybatis获取mapper的面向接口编程思想(Mapper接口动态代理实现原理)终于讲完了,感谢大家的阅读,有问题欢迎留言,指正。

路漫漫其修远兮,吾将上下而求索

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用MyBatis实现三表联合查询可以通过编写SQL语句来实现。首先,在Mapper文件中定义一个查询语句,使用JOIN语句将三个表连接起来,并指定连接条件。然后,在Java代码中调用该查询语句,将结果映射到对应的实体类中。 以下是一个示例的Mapper文件配置: ```xml <!-- 定义查询语句 --> <select id="getThreeTableData" resultMap="resultMap"> SELECT t1.*, t2.*, t3.* FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id JOIN table3 t3 ON t2.id = t3.table2_id WHERE t1.id = #{id} </select> <!-- 定义结果映射 --> <resultMap id="resultMap" type="com.example.entity.ThreeTableEntity"> <!-- 定义字段映射 --> <result property="field1" column="t1_field1"/> <result property="field2" column="t1_field2"/> <!-- 省略其他字段映射 --> <association property="table2" javaType="com.example.entity.Table2Entity"> <result property="field3" column="t2_field3"/> <!-- 省略其他字段映射 --> <association property="table3" javaType="com.example.entity.Table3Entity"> <result property="field4" column="t3_field4"/> <!-- 省略其他字段映射 --> </association> </association> </resultMap> ``` 在Java代码中调用该查询语句: ```java public ThreeTableEntity getThreeTableData(int id) { return sqlSession.selectOne("com.example.mapper.ThreeTableMapper.getThreeTableData", id); } ``` 关于MyBatis一级缓存二级缓存的配置,一级缓存是默认开启的,它是指在同一个SqlSession中,对于相同的查询语句和参数,MyBatis会将查询结果缓存起来,下次再执行相同的查询时,直接从缓存获取结果,提高查询性能。 而二级缓存是在多个SqlSession之间共享的缓存,需要手动进行配置。可以在MyBatis的配置文件中添加以下配置: ```xml <!-- 开启二级缓存 --> <settings> <setting name="cacheEnabled" value="true"/> </settings> <!-- 配置二级缓存 --> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> ``` 需要注意的是,使用二级缓存时,需要确保查询的结果是可序列化的,并且实体类需要实现Serializable接口

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值