Mybatis之数据库操作流程解析

本文深入解析Mybatis的数据库操作流程,从映射类获取到动态代理对象,再到MapperMethod的execute方法,详细阐述了select操作的执行过程,包括参数封装、SQL执行、缓存机制及防止SQL注入的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectByPrimaryKey(1);

这篇文章我们将从上面两行代码看起,首先就是获得mapper文件对应的映射类,然后执行里面的数据库操作:

public <T> T getMapper(Class<T> type) {
    return configuration.<T>getMapper(type, this);
  }

在方法里面将映射类的class对象和当前session作为参数在configuration里面查找:

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

然后从存放mapper的工厂里面来查找:

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
	//根据class对象获取它对应的代理工厂
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
    //由代理工厂生成一个代理对象
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

由代理工厂为我们生成一个对应接口的代理对象:

public T newInstance(SqlSession sqlSession) {
	//封装成一个代理类
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    //作为参数实例化
    return newInstance(mapperProxy);
  }

将当前的会话、接口、和方法的缓存封装成一个mapper代理类,然后实例化:

protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

实际上刚刚创建的MapperProxy就是一个拦截器,那么我们所关注的点就是它里面的invoke方法:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
    	//当前代理类是否是执行方法的声明类,如果是,直接执行
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
        //如果是默认的方法,则按照执行默认方法的逻辑处理
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

首先会判断声明当前方法的是否是一个类,如果不是就需要创建一个MapperMethod :

public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    switch (command.getType()) {
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值