mybatis源码解读(五)——sql语句的执行流程

时间:2022-10-27 16:52:09

  还是以第一篇博客中给出的例子,根据代码实例来入手分析。

     static {
InputStream inputStream = MybatisTest.class.getClassLoader().getResourceAsStream("mybatis-configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} /**
* 查询单个记录
*/
@Test
public void testSelectOne() {
SqlSession session = sqlSessionFactory.openSession();
User user = session.selectOne(NAME_SPACE + ".selectUserById", 1);
System.out.println(user);
session.close();
}

  如何加载配置文件前面也已经介绍了,通过配置文件产生SqlSessionFactory,追溯源码可以发现其实现是 DefaultSqlSessionFactory。

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

  得到 SqlSessionFactory 之后,就可以通过 SqlSessionFactory 去获取 SqlSession 对象。源码如下:

     @Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
} private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
//Environment对象封装了配置文件中对于数据源和事务的配置
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//获取Executor对象,用来执行sql语句
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();
}
}

  这里我们重点看一下第 15 行代码:

   public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

  根据执行器类型这里有多种不同的执行器Executor。

  注意第 12 行代码,如果我们开启了缓存,即 cacheEnabled = true(这里是一级缓存,默认是开启的),第13行代码使用了装饰器模式,在原有的 Executor 上装饰了缓存功能。

  第 15 行用于设置插件。

  这时候已经得到SqlSession对象了,实际类型是 DefaultSqlSession。接下来我们就可以通过该对象来执行sql语句了。

1、insert 操作

     /**
* 插入一条记录
*/
@Test
public void testInsert() {
SqlSession session = sqlSessionFactory.openSession();
User user = new User(2, "zhangsan", 22);
session.insert(NAME_SPACE + ".insertUser", user);
session.commit();
session.close();
}

  通过第8行代码,我们进入到 insert 方法:

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

  注意:这里通过 insert 方法,调用的是 update 方法。

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

  第4行根据给的statement参数,获取配置的所有如下信息,并将其封装到 MappedStatement 对象中,关于这个对象后面会详细介绍。

     <!-- 向 user 表插入一条数据 -->
<insert id="insertUser" parameterType="com.ys.po.User" >
insert into
user(<include refid="Base_Column_List" />)
value(#{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},#{age,jdbcType=INTEGER})
</insert>

  ①、接着我们看第 5 行代码,首先看 wrapCollection(parameter) 方法:

     private Object wrapCollection(final Object object) {
if (object instanceof Collection) {
DefaultSqlSession.StrictMap<Object> map = new DefaultSqlSession.StrictMap<Object>();
map.put("collection", object);
if (object instanceof List) {
map.put("list", object);
}
return map;
} else if (object != null && object.getClass().isArray()) {
DefaultSqlSession.StrictMap<Object> map = new DefaultSqlSession.StrictMap<Object>();
map.put("array", object);
return map;
}
return object;
}

  通过这段代码的if-else if 语句主要做了如下两个操作:

  1、如果传入的参数是集合 Collection,在 map 集合中放入一个key为"collection"、value为参数的键值对,接着判断该集合是不是 List 类型,如果是,那么在 map 集合中在放入一个key为"list"、value为参数的键值对。

  2、如果传入的参数是数组类型,那么在 map 中放入一个key为"array"、value为参数的键值对。

  注意:这里的 StrictMap ,其实就是一个 HashMap。

   public static class StrictMap<V> extends HashMap<String, V> {

     private static final long serialVersionUID = -5741767162221585340L;

     @Override
public V get(Object key) {
if (!super.containsKey(key)) {
throw new BindingException("Parameter '" + key + "' not found. Available parameters are " + this.keySet());
}
return super.get(key);
} } }

  ②、wrapCollection(parameter) 方法介绍完了。接着我们看 executor.update()方法:

  这里需要说明的是 Executor 对象上面我们已经介绍了,由于默认是开启一级缓存的,这时候我们进入 CachingExecutor 类的 update() 方法:

   public int update(MappedStatement ms, Object parameterObject) throws SQLException {
flushCacheIfRequired(ms);
return delegate.update(ms, parameterObject);
}

  首先我们看这里的第 2 行代码:

  private void flushCacheIfRequired(MappedStatement ms) {
Cache cache = ms.getCache();
if (cache != null && ms.isFlushCacheRequired()) {
tcm.clear(cache);
}
}

  这里表示的意思是是否清除缓存。看我们是否在配置文件中配置了 <cache> 标签,以及我们是否在 <insert /> 标签中是否增加了 flushCache="true"属性。如果有其中任何一个,此次操作都会清除缓存。

  接着我们再看第3行代码,这里的delegate 是 Executor,但是这是一个接口,其真实类型是SimpleExecutor,经过装饰器模式,调用 CachingExecutor 的 update 方法,经过处理后,最后最后调用 SimpleExecutor的update方法:

  具体调用:

  首先调用 BaseExecutor 的 update 方法

  public int update(MappedStatement ms, Object parameter) throws SQLException {
ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());
if (closed) {
throw new ExecutorException("Executor was closed.");
}
clearLocalCache();
return doUpdate(ms, parameter);
}

  然后调用 doUpdate 方法,由于 SimpleExecutor 继承 BaseExecutor 类,并重写了 doUpdate 方法,我们看 SimpleExecutor 类的 doUpdate 方法:

   public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.update(stmt);
} finally {
closeStatement(stmt);
}
}

  看到这里,Statement 对象,看到我们熟悉的 JDBC 操作数据库的对象了吧。我们直接看第 6 行代码:

   private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
Connection connection = getConnection(statementLog);
stmt = handler.prepare(connection, transaction.getTimeout());
handler.parameterize(stmt);
return stmt;
}

  第 3 行代码获取数据库连接,是根据前面配置的数据源来获取。接着我们看 handler.update(stemt) 方法:

   public int update(Statement statement) throws SQLException {
String sql = boundSql.getSql();
Object parameterObject = boundSql.getParameterObject();
KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
int rows;
if (keyGenerator instanceof Jdbc3KeyGenerator) {
statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
rows = statement.getUpdateCount();
keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
} else if (keyGenerator instanceof SelectKeyGenerator) {
statement.execute(sql);
rows = statement.getUpdateCount();
keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
} else {
statement.execute(sql);
rows = statement.getUpdateCount();
}
return rows;
}

  这里就都是我们熟悉的 JDBC 操作了。

2、update 和 delete 操作

     /**
* 更新一条记录
*/
@Test
public void testUpdate() {
SqlSession session = sqlSessionFactory.openSession();
User user = new User(2, "lisi", 22);
session.update(NAME_SPACE + ".updateUserById", user);
session.commit();
session.close();
} /**
* 删除一条记录
*/
@Test
public void testDelete() {
SqlSession session = sqlSessionFactory.openSession();
session.delete(NAME_SPACE + ".deleteUserById", 2);
session.commit();
session.close();
}

  进入到上述第 8 行和 第 19 行代码,我们发现都是进入到和 上面 insert 操作一样的代码:

  第 8 行:

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

  第 19 行:

  @Override
public int delete(String statement, Object parameter) {
return update(statement, parameter);
}

  之后的 update 也是上面的代码。这也和我们理解的应该保持一致。

  结论:

insert、update、delete都是属于对数据库的行进行更新操作

  所以这三种语句的执行都是采用的同种逻辑处理。最终都可以调用 executeUpdate() 方法来处理。唯一不同的是 select 操作,必须要调用 executeQuery() 来执行。

3、select 操作

     /**
* 查询单个记录
*/
@Test
public void testSelectOne() {
SqlSession session = sqlSessionFactory.openSession();
User user = session.selectOne(NAME_SPACE + ".selectUserById", 1);
System.out.println(user);
session.close();
} /**
* 查询多个记录
*/
@Test
public void testSelectList() {
SqlSession session = sqlSessionFactory.openSession();
List<User> listUser = session.selectList(NAME_SPACE + ".selectUserAll");
if (listUser != null) {
System.out.println(listUser.size());
}
session.close();
}

  首先看第 7 行代码:

   public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}

  看到上面的第 3 行代码,我们可能马上就明白了,其实selectOne() 和 selectList() 也都是调用的 selectList() 方法,只不过 selectOne() 是获取集合的第一个元素而已。

  接着看 selectList() 源码:

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

  看第10的 query 方法:

   public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
BoundSql boundSql = ms.getBoundSql(parameterObject);
CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
   public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
throws SQLException {
Cache cache = ms.getCache();
if (cache != null) {
flushCacheIfRequired(ms);
if (ms.isUseCache() && resultHandler == null) {
ensureNoOutParams(ms, parameterObject, boundSql);
@SuppressWarnings("unchecked")
List<E> list = (List<E>) tcm.getObject(cache, key);
if (list == null) {
list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
tcm.putObject(cache, key, list); // issue #578 and #116
}
return list;
}
}
return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}

  最后我们来到doQuery() 方法:

  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
   @Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
return resultSetHandler.<E> handleResultSets(ps);
}

  至此,select 操作也执行完毕了。