mybatis源码解析

获取流解析

InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

调用getResourceAsStream(String resource)方法

public static InputStream getResourceAsStream(String resource) throws IOException {
    return getResourceAsStream(null, resource);
  }

调用getResourceAsStream(ClassLoader loader, String resource)方法

 public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
    InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
    if (in == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return in;
  }

通过classLoaderWrapper.getResourceAsStream(resource, loader)方法来获取流,我们再继续跟进getResourceAsStream(resource, loader)方法

  public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
    return getResourceAsStream(resource, getClassLoaders(classLoader));
  }

再继续跟进getResourceAsStream(resource,getClassLoaders(classLoader))方法

InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {
    for (ClassLoader cl : classLoader) {
      if (null != cl) {

        // try to find the resource as passed
        InputStream returnValue = cl.getResourceAsStream(resource);

        // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
        if (null == returnValue) {
          returnValue = cl.getResourceAsStream("/" + resource);
        }

        if (null != returnValue) {
          return returnValue;
        }
      }
    }
    return null;
  }

获取SqlSessionFactory对象解析


SqlSessionFactory sqlsessionfactor = new SqlSessionFactoryBuilder().build(is);
 public SqlSessionFactory build(InputStream inputStream) {
    return build(inputStream, null, null);
  }

调用build(inputStream, null, null);方法

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        inputStream.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }

跟进build(parser.parse())方法

public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

获取SqlSession对象解析

SqlSession session = sqlsessionfactor.openSession();

SqlSessionFactory创建的时候实际上返回的是一个DefaultSqlSessionFactory对象

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

获取Mapper对象解析

UserMapper mapper = session.getMapper(UserMapper.class);

需要进入到getMapper()方法中去,这里调用的是DefaultSqlSession中的getMapper()
来获取UserMapper的时候,实际上是从configuration当中的MapperRegistry当中获取UserMapper的代理对象:

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    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);
    }
  }

knownMappers属性里面的值,实际上就是我们在mappers扫描与解析的时候放进去的。

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

public T newInstance(SqlSession sqlSession) {
  final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
  return newInstance(mapperProxy);
}

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

分页查询

结果展示
在这里插入图片描述

测试类

public class Test {
	 public static void main(String[] args) throws IOException { String resource="mybatis-config.xml";
     InputStream is= Resources.getResourceAsStream(resource);
     SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
     SqlSession session= sqlSessionFactory.openSession();
     T7mapper mapper= session.getMapper(T7mapper.class);
       Map<String,Object> map = new HashMap<>();
       map.put("startIndex",1);
       map.put("pageSize",1);
       List<T7> content=mapper.queryLimit(map);
      System.out.println(content);
	 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值