Mybatis源码笔记一

mybatis访问数据库的步骤如下:

1、创建SqlSessionFactory
2、再使用SqlSessionFactory的openSession()获得SqlSession

3、通过SqlSession执行相关sql操作

4、关闭连接

SqlSessionFactory是通过SqlSessionFactoryBuilder的build方法创建的,build方法有很多重载的方法。

看源码知道最终都会调用如下方法,返回的是DefaultSqlSessionFactory

public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }
public  T selectOne(String statement) {
    return selectOne(statement, null);
  }

  public  T selectOne(String statement, Object parameter)
  {
    List list = selectList(statement, parameter);
    if (list.size() == 1)
      return list.get(0);
    if (list.size() > 1) {
      throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
    }
    return null;
  }

 openSession()后最终执行如下方法,通过Configuration来创建Executor执行器,Executor执行器也是最终与数据库直接交互的接口。返回的是一个DefaultSqlSession 
  
 private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      Environment environment = this.configuration.getEnvironment();
      TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      Executor executor = this.configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(this.configuration, executor);
    } catch (Exception e) {
      closeTransaction(tx);
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }
接下来通过SqlSession的相关方法,执行sql返回结果,可以看到selectOne最终自行的还是selectList方法。

public  T selectOne(String statement) {
    return selectOne(statement, null);
  }

  public  T selectOne(String statement, Object parameter)
  {
    List list = selectList(statement, parameter);
    if (list.size() == 1)
      return list.get(0);
    if (list.size() > 1) {
      throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
    }
    return null;
  }
selectList方法通过Configuration的处理从xml配置文件中获取相关的sql语句,然后让Exector这个执行器来执行相关的sql语句返回结果。

同样insert和delete最终调用的还是update,同样通过Configuration的处理从xml配置文件中获取相关的sql语句,然后让Exector这个执行器来执行相关的sql语句返回结果。

public  List selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
      MappedStatement ms = this.configuration.getMappedStatement(statement);
      List result = this.executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
      return result;
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }
public int insert(String statement) {
    return insert(statement, null);
  }

  public int insert(String statement, Object parameter) {
    return update(statement, parameter);
  }

  public int update(String statement) {
    return update(statement, null);
  }

  public int update(String statement, Object parameter) {
    try {
      this.dirty = true;
      MappedStatement ms = this.configuration.getMappedStatement(statement);
      return this.executor.update(ms, wrapCollection(parameter));
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error updating database.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

执行器Executor中使用Configuration创建对应的StatementHandler来创建Statement来执行相关sql语句




你可能感兴趣的:(java)