首页 文章详情

手写一个Mybatis?

IT可乐 | 299 2021-08-24 07:00 0 0 0
UniSMS (合一短信)

在文章内容开始之前,给大家送个福利,将自己整理了好多年的浏览器收藏夹送给大家,太多干货了。将下载后的 html 直接导入到你们的浏览器书签中,就成你们的啦。【文末有获取方式】

本篇文章,可乐将为大家介绍通过接口代理的方式去执行SQL操作,了解了这种方式,手写 Mybatis 框架不是梦。话不多说,直接上图:

其实无论哪种方式,我们最终是需要找到对应的 SQL 语句,接口代理的方式就是通过 【包名.方法名】 的方式,去找到 xxxMapper.xml 文件中的 SQL 语句。

很明显,通过动态代理的方式,我们能够实现该功能。下面,可乐将为大家手撸一个 Mybatis 的接口代理。实例源码地址:https://github.com/YSOcean/mybatisproject

1、创建接口

package com.itcoke.mapperproxy;

import com.itcoke.bean.Person;

public interface PersonMapper {

    Person selectPersonById(Long pid);
}

2、创建代理类

package com.itcoke.mapperproxy;

import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MapperProxyHandler implements InvocationHandler {
    private SqlSession sqlSession;
    private Class<?> targetInterface;

    public MapperProxyHandler(SqlSession sqlSession,Class<?> targetInterface){
        this.sqlSession = sqlSession;
        this.targetInterface = targetInterface;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String className = targetInterface.getName();
        String methodName = method.getName();
        String statement = className + "." + methodName;

        return sqlSession.selectOne(statement,args[0]);
    }
}

3、创建代理工厂类

package com.itcoke.mapperproxy;

import java.lang.reflect.Proxy;

public class MapperProxyFactory {
    private Class<?> targetInterface;

    public MapperProxyFactory(Class<?> targetInterface){
        this.targetInterface = targetInterface;
    }

    public Object newInstance(MapperProxyHandler handler){
        return Proxy.newProxyInstance(targetInterface.getClassLoader(),
                new Class[]{targetInterface},
                handler);
    }
}

4、创建测试类

package com.itcoke.mapperproxy;

import com.itcoke.bean.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperProxyTest {

    public static void main(String[] args) {
        // 1、获取目标接口对象
        Class<?> targetInterface = PersonMapper.class;
        // 2、获取 SqlSession 对象
        SqlSession sqlSession = getSqlSession();
        MapperProxyHandler proxyHandler = new MapperProxyHandler(sqlSession,targetInterface);
        MapperProxyFactory mapperProxyFactory = new MapperProxyFactory(PersonMapper.class);
        PersonMapper personMapper = (PersonMapper)mapperProxyFactory.newInstance(proxyHandler);
        Person person = personMapper.selectPersonById(1L);
        System.out.println(person);
    }



    public static SqlSession getSqlSession() {
        //定义mybatis全局配置文件
        String resource = "mybatis-config.xml";
        //加载 mybatis 全局配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return  sessionFactory.openSession();
    }
}

5、总结

其实 Mybatis 内部实现方式大体上和上面差不多,在加入一些类型处理器,这就是一个简易版本的 Mybatis 了。

6、福利

前面说的我整理的浏览器收藏夹,只需要在本公众号回复【收藏夹】即可领取。


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter