自己动手写一个Mybatis框架

完整代码GitHub地址:https://github.com/Sabot1203/mybatis_design

mybatis在使用代理dao的方式实现访问数据库是主要是创建代理对象,并在对象中调用具体的增删改查方法。
在自定义框架时,用到的知识包括但不限于:工厂模式(Factory 工厂模式)、构造者模式(Builder 模式)、代理模式,反射,自定义注解,注解的反射,xml 解析,数据库元数据,元数据的反射等。

		//1.读取配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建 SqlSessionFactory 的构建者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.使用构建者创建工厂对象 SqlSessionFactory
        SqlSessionFactory factory = builder.build(in);
        //4.使用 SqlSessionFactory 生产 SqlSession 对象
        SqlSession session = factory.openSession();
        //5.使用 SqlSession 创建 dao 接口的代理对象
        IUserDao userDao = session.getMapper(IUserDao.class);
        //6.使用代理对象执行查询所有方法
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        //7.释放资源
        session.close();
        in.close();

仿照mybatis的实现流程,删去Maven中的mybatis依赖坐标,自己实现流程中需要使用的类。

读取配置文件 --> 使用工厂生产SQLSession --> 使用SqlSession创建代理对象 --> 使用代理对象访问数据库。

这里的核心结构为
自定义的mybatis框架结构
以下为一次完整的查询操作流程图:
在这里插入图片描述
部分关键代码:

 /**
     * @param:Class<T> daoInterfaceClass
     * @return: T
     * @description:用于创建代理对象
     */
    public <T> T getMapper(Class<T> daoInterfaceClass) {

        //创建代理
        return (T) Proxy.newProxyInstance(daoInterfaceClass.getClassLoader(), 
        	new Class[]{daoInterfaceClass},new MapperProxy(cfg.getMappers(),coon));

    }

    /**
     * @param:Object proxy, Method method, Object[] args
     * @return:
     * @description:用于对方法进行增强
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //获取方法名
        String methodName = method.getName();
        //获取方法所在类名
        String className = method.getDeclaringClass().getName();
        //组合成key从Map中取出对应的Mapper
        String key = className+"."+methodName;
        Mapper mapper = mappers.get(key);
        if (mapper == null){
            throw new IllegalArgumentException("参数有误");
        }
        //调用工具类执行增删改查方法
        return new Executor().selectList(mapper,coon);

    }
/**
 * @auther:sabot
 * @date:2020/04/18
 * @description:用于创建数据源的工具类
 */
public class DateSourceUtil {

    public static Connection getConnection(Configuration cfg){
        try {
            Class.forName(cfg.getDriver());
            return DriverManager.getConnection(cfg.getUrl(),cfg.getUsername(),cfg.getPassword());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值