Mybatis(一)

最近打算先过下Mybatis的源码,暂时将Spring置后,预计六月底完成Mybatis的源码梳理,到时再投Spring。目前所构建的项目是maven工程,在编写过程中遵循最小依赖的原则,整个过程会针对Mybatis慢慢丰富。

Mybatis的核心就是代理工厂模式,先来聊聊什么是代理工厂模式

代理工厂模式是一种常见的设计模式,它通过在类之间引入一个代理层来控制对对象的访问。在该模式中,代理类充当了客户端与目标对象之间的中介者,用于隐藏目标对象的实现细节,并提供了额外的功能,如控制对目标对象的访问、缓存目标对象、保护目标对象等。

那么代理工厂模式的实现逻辑又是什么样子的?

代理工厂模式通常包含两个部分:一个是用于生成代理对象的工厂类,另一个是代理对象本身。工厂类通过接收客户端的请求并生成相应的代理对象,从而将客户端代码与目标对象的实现解耦。

代理工厂类在Mybatis中的实现

代理对象的实现

/**
 * 方法实现Serializable的意义:Serializable接口是一种标注,表示当前对象可以被序列化并且可以存储到硬盘上——可以这么去进行理解
 *
 * @param <T>
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {

    private static final long serializableVersionUID = -6424540398559729838L;

    private Map<String, String> sqlSession;
    private Class<T> mapperInterface;

    public MapperProxy(Map<String, String> sqlSession, Class mapperInterface){
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }

    //比较方法对应的class字节码文件与Object对象对应的字节码文件是否相同——当相同时使用invoke
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (Object.class.equals(method.getDeclaringClass())){
            return method.invoke(this, args);
        }else {
            return "你的被代理了" + sqlSession.get(mapperInterface.getName()) + "." + method.getName();
        }
    }
}

这个类本质就是一个代理处理器类,用于处理方法的调用,这块做的事情是加载配数据库的配置与读取接口类,具体操作是在invoke中实现,后续添加。

代理对象工厂类的实现

public class MapperProxyFactory<T> {

    private final Class<T> mapperInterface;

    public MapperProxyFactory(Class<T> mapperInterface){
        this.mapperInterface = mapperInterface;
    }

    public T newInstance(Map<String, String> sqlSession){
        //根据接口会话和class字节类生成对应的mapper代理类
        final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }

}

 入参为sqlSession,生成代理对象,主要实现是newProxyInstance()方法根据传入的类加载器、接口列表和代理处理器对象来生成代理类的字节码,并通过反射机制将其加载到内存中。最后,返回一个代理类对象,其类型为指定的接口类型。三个参数分别是

  • mapperInterface.getClassLoader(): 指定代理类的 Classloader,用于加载代理类
  • new Class[]{mapperInterface}: 指定代理类需要实现的接口列表,即指定要为哪个接口生成代理类
  • mapperProxy: 指定代理类的调用处理器,用于处理接口方法的调用。

具体测试代码如下

    @Test
    public void testProxy() {
        MapperProxyFactory mapperProxyFactory = new MapperProxyFactory(IUserMapper.class);

        Map<String, String> sqlSession = new HashMap<>();
        sqlSession.put("IUserMapper", "getUserById");
        sqlSession.put("IUserMapper", "getUserByName");

        IUserMapper o = (IUserMapper) mapperProxyFactory.newInstance(sqlSession);
        System.out.println(o.getUserById(1));
        System.out.println(o.getUserByName("test"));
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值