淺談MyBatis 如何執(zhí)行一條 SQL語句
前言
Mybatis 是 Java 開發(fā)中比較常用的 ORM 框架。在日常工作中,我們都是直接通過 Spring Boot 自動(dòng)配置,并直接使用,但是卻不知道 Mybatis 是如何執(zhí)行一條 SQL 語句的,而這篇文章就是來揭開 Mybatis 的神秘面紗。
基礎(chǔ)組件
我們要理解 Mybatis 的執(zhí)行過程,就必須先了解 Mybatis 中都有哪一些重要的類,這些類的職責(zé)都是什么?
SqlSession
我們都很熟悉,它對(duì)外提供用戶和數(shù)據(jù)庫之間交互需要使用的方法,隱藏了底層的細(xì)節(jié)。它默認(rèn)是實(shí)現(xiàn)類是 DefaultSqlSession
Executor
這個(gè)是執(zhí)行器,SqlSession 中對(duì)數(shù)據(jù)庫的操作都是委托給它。它有多個(gè)實(shí)現(xiàn)類,可以使用不同的功能。

Configuration
它是一個(gè)很重要的配置類,它包含了 Mybatis 的所有有用信息,包括 xml 配置,動(dòng)態(tài) sql 語句等等,我們到處都可以看到這個(gè)類的身影。
MapperProxy
這是一個(gè)很重要的代理類,它代理的就是 Mybatis 中映射 SQL 的接口。也就是我們常寫的 Dao 接口。
工作流程
初步使用
首先,我們需要得到一個(gè) SqlSessionFactory 對(duì)象,該對(duì)象的作用是可以獲取 SqlSession 對(duì)象。
// 讀取配置
InputStream resourceAsStream = Resources.getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 創(chuàng)建一個(gè) SqlSessionFactory 對(duì)象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
當(dāng)我們得到一個(gè) SqlSessionFactory 對(duì)象之后,就可以通過它的 openSession 方法得到一個(gè) SqlSession 對(duì)象。
SqlSession sqlSession = sqlSessionFactory.openSession(true);
最后,我們通過 SqlSession 對(duì)象獲取 Mapper ,從而可以從數(shù)據(jù)庫獲取數(shù)據(jù)。
// 獲取 Mapper 對(duì)象 HeroMapper mapper = sqlSession.getMapper(HeroMapper.class); // 執(zhí)行方法,從數(shù)據(jù)庫中獲取數(shù)據(jù) Hero hero = mapper.selectById(1);
詳細(xì)流程
獲取 MapperProxy 對(duì)象
我們現(xiàn)在主要關(guān)注的就是 getMapper 方法,該方法為我們創(chuàng)建一個(gè)代理對(duì)象,該代理對(duì)象為我們執(zhí)行 SQL 語句提供了重要的支持。
// SqlSession 對(duì)象
@Override
public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}
getMapper 方法里面委托 Configuration 對(duì)象去獲取對(duì)應(yīng)的 Mapper 代理對(duì)象,之前說過 Configuration 對(duì)象里面包含了 Mybatis 中所有重要的信息,其中就包括我們需要的 Mapper 代理對(duì)象,而這些信息都是在讀取配置信息的時(shí)候完成的,也就是執(zhí)行sqlSessionFactoryBuilder.build 方法。
// Configuration 對(duì)象
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
我們可以看到它又將獲取 Mapper 代理對(duì)象的操作委托給了 MapperRegistry 對(duì)象(擱著俄羅斯套娃呢?),這個(gè) MapperRegistry 對(duì)象里面就存放了我們想要的 Mapper 代理對(duì)象,如果你這么想,就錯(cuò)了,實(shí)際上,它存放的并不是我們想要的 Mapper 代理對(duì)象,而是 Mapper 代理對(duì)象的工廠,Mybatis 這里使用到了工廠模式。
public class MapperRegistry {
private final Configuration config;
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
public MapperRegistry(Configuration config) {
this.config = config;
}
@SuppressWarnings("unchecked")
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);
}
}
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}
}
我只保留了 getMapper 方法和 addMapper 方法。
在 getMapper 方法中,它獲取的是 MapperProxyFactory 對(duì)象,我們通過名稱可以得出這是一個(gè) Mapper 代理對(duì)象工廠,但是我們是要得到一個(gè) MapperProxy 對(duì)象,而不是一個(gè)工廠對(duì)象,我們?cè)賮砜?getMapper 方法,它通過 mapperProxyFactory.newInstance 來創(chuàng)建代理對(duì)象。
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<>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
創(chuàng)建了一個(gè) MapperProxy 對(duì)象,并且通過 Proxy.newProxyInstance 方法(不會(huì)還有人不知道這是 JDK 動(dòng)態(tài)代理吧),創(chuàng)建一個(gè)代理對(duì)象處理,這個(gè)代理對(duì)象就是我們想要的結(jié)果。這里沒有體現(xiàn)出來代理了哪個(gè)對(duì)象啊?其實(shí) mapperInterface 這是一個(gè)成員變量,它引用了需要被代理的對(duì)象。而這個(gè)成員變量實(shí)在創(chuàng)建 MapperProxyFactory 對(duì)象的時(shí)候賦值的,所以我們每一個(gè)需要被代理的接口,在 Mybatis 中都會(huì)為它生成一個(gè) MapperProxyFactory 對(duì)象,該對(duì)象的作用就是為了創(chuàng)建所需要的代理對(duì)象。

緩存執(zhí)行方法
當(dāng)我們獲取到代理對(duì)象 mapper 之后,就可以執(zhí)行它里面的方法。
這里使用一個(gè)例子:
// Myabtis 所需要的接口
public interface HeroMapper {
Hero selectById(Integer id);
}
// HeroMapper 接口所對(duì)應(yīng)的 xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.HeroMapper">
<select id="selectById" resultType="test.Hero">
select * from hero where id = #{id}
</select>
</mapper>
我們執(zhí)行 selectById 方法,獲取一個(gè)用戶的信息。
// 獲取 Mapper 對(duì)象 HeroMapper mapper = sqlSession.getMapper(HeroMapper.class); // 執(zhí)行方法,從數(shù)據(jù)庫中獲取數(shù)據(jù) Hero hero = mapper.selectById(1);
通過上面的解析已經(jīng)知道,這里的 mapper 是一個(gè)代理對(duì)象的引用,而這個(gè)代理類則是 MapperProxy,所以我們主要是去了解 MapperProxy 這個(gè)代理類做了什么事情。
public class MapperProxy<T> implements InvocationHandler, Serializable {
private final SqlSession sqlSession;
private final Class<T> mapperInterface;
private final Map<Method, MapperMethodInvoker> methodCache;
public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethodInvoker> methodCache) {
this.sqlSession = sqlSession;
this.mapperInterface = mapperInterface;
this.methodCache = methodCache;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, args);
} else {
return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
return methodCache.computeIfAbsent(method, m -> {
return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
}
}
private static class PlainMethodInvoker implements MapperMethodInvoker {
private final MapperMethod mapperMethod;
public PlainMethodInvoker(MapperMethod mapperMethod) {
super();
this.mapperMethod = mapperMethod;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
return mapperMethod.execute(sqlSession, args);
}
}
}
代理對(duì)象執(zhí)行方法時(shí)都是直接執(zhí)行 invoke() 方法,在這個(gè)方法中,我們主要就看一條語句 cachedInvoker(method).invoke(proxy, method, args, sqlSession);
我們首先看 cachedInvoker 方法,它的參數(shù)是 Method 類型,所以這個(gè) method 表示的就是我們執(zhí)行的方法 HeroMapper.selectById,它首先從緩存中獲取是否之前已經(jīng)創(chuàng)建過一個(gè)該方法的方法執(zhí)行器 PlainMethodInvoker 對(duì)象,其實(shí)這只是一個(gè)包裝類,可有可無,在工程上來說,有了這個(gè)包裝類,會(huì)更加易于維護(hù)。而這個(gè)執(zhí)行器里面只有一個(gè)成員對(duì)象,這個(gè)成員對(duì)象就是 MapperMethod,并且這個(gè) MapperMethod 的構(gòu)造函數(shù)中需要傳遞 HeroMapper、HeroMapper.selectById、Cofiguration 這三個(gè)參數(shù)。
以上步驟都執(zhí)行完成之后,接下來我們可以看到執(zhí)行了 PlainMethodInvoker 的 invoke 方法,而它又將真正的操作委托給了 MapperMethod,執(zhí)行 MapperMethod 下的 execute 方法,這個(gè)方法就是本文章的重點(diǎn)所在。

構(gòu)造參數(shù)
從上面的解析可以知道,最后會(huì)執(zhí)行到這個(gè)方法之中。
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
case SELECT:
if (method.returnsVoid() && method.hasResultHandler()) {
executeWithResultHandler(sqlSession, args);
result = null;
} else if (method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else if (method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else if (method.returnsCursor()) {
result = executeForCursor(sqlSession, args);
} else {
Object param = method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(command.getName(), param);
if (method.returnsOptional()
&& (result == null || !method.getReturnType().equals(result.getClass()))) {
result = Optional.ofNullable(result);
}
}
break;
case FLUSH:
result = sqlSession.flushStatements();
break;
default:
throw new BindingException("Unknown execution method for: " + command.getName());
}
return result;
}
這個(gè)方法中,我們可以看到熟悉的幾個(gè)關(guān)鍵字:select、update、delete、insert,這個(gè)就是為了找到執(zhí)行方式,我們因?yàn)槭?select 語句,所以分支會(huì)走向 select,并且最終會(huì)執(zhí)行到 sqlSession.selectOne 方法中,所以最終饒了一大圈,依然還是回到了我們一開始就提到的 SqlSession 對(duì)象中。
在這個(gè)方法中,首先會(huì)構(gòu)造參數(shù),也就是我們看到的 convertArgsToSqlCommandParam 方法,它的內(nèi)部執(zhí)行方式是按照如下方式來轉(zhuǎn)換參數(shù)的:
使用 @param 自定義命名
amethod(@Param int a, @Param int b) 則會(huì)構(gòu)造 map -> [{"a", a_arg}, {"b", b_arg}, {"param1", a_arg}, {"param2", b_arg}],a 和 param1 是對(duì)參數(shù) a 的命名,a_arg 是傳遞的實(shí)際的值。
雖然只有兩個(gè)參數(shù),但是最后卻會(huì)在 Map 存在四個(gè)鍵值對(duì),因?yàn)?Mybatis 最后自己會(huì)生成以 param 為前綴的參數(shù)名稱,名稱按照參數(shù)的位置進(jìn)行命名。
不使用 @param
amethod(int a, int b),則會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"arg1", b_arg}, {"param1", a_arg}, {"param2", b_arg}],因?yàn)闆]有對(duì)參數(shù)進(jìn)行自定義命名,所以 Myabtis 就對(duì)參數(shù)取了一個(gè)默認(rèn)的名稱,以 arg 為前綴,位置為后綴進(jìn)行命名。
在參數(shù)只有一個(gè),并且參數(shù)為集合的情況下,會(huì)存放多個(gè)鍵值對(duì):
- amethod(Collection<Integer> a),這種情況下,會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"collection", a_arg}]
- amethod(List<Integer> a),這種情況下,會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"collection", a_arg}, {"list", a_arg}]
- amethod(Integer[] a),這種情況下,會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"array", a_arg}]
- 但是,如果有兩個(gè)參數(shù),那么就不會(huì)這么存放,而是按照常規(guī)的方式:
- amethod(List<Integer> a,List<Integer> b) 則會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"arg1", b_arg}, {"param1", a_arg}, {"param2", b_arg}]
- amethod(List<Integer> a,int b) 則會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"arg1", b_arg}, {"param1", a_arg}, {"param2", b_arg}]
不會(huì)作為參數(shù)的對(duì)象
在 Mybatis 中有兩個(gè)特殊的對(duì)象:RowBounds、ResultHandler,這兩個(gè)對(duì)象如果作為參數(shù)則不會(huì)放入到 map 中,但是會(huì)占據(jù)位置。
amethod(int a,RowBounds rb, int b),這種情況下,會(huì)構(gòu)造 map -> [{"arg0", a_arg}, {"arg2", b_arg}, {"param1", a_arg}, {"param2", b_arg}]
注意這里的 b 參數(shù)的命名分別是 arg2 和 param2,arg2 是因?yàn)樗奈恢迷趨?shù)的第 3 位,而 param2 則是因?yàn)樗堑?2 個(gè)有效參數(shù)。
獲取需要執(zhí)行的 SQL 對(duì)象
參數(shù)構(gòu)造完成之后,我們就需要尋找需要執(zhí)行的 SQL 語句了。
@Override
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.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;
}
}
這里的 statement 雖然是 String 類型的,但是它并不是真正的 SQL 語句,它是一個(gè)尋找對(duì)應(yīng) MapperStatement 對(duì)象的名稱,在我們的例子中,它就是 test.HeroMapper.selectById ,Mybatis 通過這個(gè)名稱可以尋找到包含了 SQL 語句的對(duì)象。
我們跟蹤代碼的執(zhí)行,最后會(huì)來到下面這個(gè)方法,這是一個(gè)包含三個(gè)參數(shù)的重載方法。
@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();
}
}
在第四行代碼中,可以得知它通過 statement 從 Configuration 對(duì)象中獲取了一個(gè) MapperStatement 對(duì)象, MapperStatement 對(duì)象包含的信息是由 <select>、<update>、<delete> 、<insert> 元素提供的,我們?cè)谶@些元素中定義的信息都會(huì)保存在該對(duì)象中,如:Sql 語句、resultMap、fetchSize 等等。
執(zhí)行 SQL 語句
獲取到包含 SQL 語句信息的對(duì)象之后,就會(huì)交給 Execute 執(zhí)行器對(duì)象去執(zhí)行后續(xù)的處理,也就是 executor.query 方法。
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
BoundSql boundSql = ms.getBoundSql(parameter);
CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
獲取需要自行的 Sql 語句,然后創(chuàng)建一個(gè)緩存使用的 key,用于二級(jí)緩存。
@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
// ....
// 跟緩存有關(guān),如果緩存中存在數(shù)據(jù),則直接從緩存中返回,否則從數(shù)據(jù)庫中查詢
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
return list;
}
最后會(huì)執(zhí)行到一個(gè) doQuery 方法
@Override
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.query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
這段代碼創(chuàng)建了一個(gè) Statement 對(duì)象的處理器 StatementHandler,這個(gè)處理器主要的工作就是完成 JDBC 中 PrepareStatement 對(duì)象的一些準(zhǔn)備工作,包括:創(chuàng)建 PrepareStatement 對(duì)象,設(shè)置需要執(zhí)行的 sql 語句,為 sql 語句中的參數(shù)賦值。完成這些工作之后,就開始從數(shù)據(jù)庫獲取數(shù)據(jù)了。
@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
return resultSetHandler.handleResultSets(ps);
}
第四行代碼即執(zhí)行對(duì)應(yīng)的 Sql 查詢,后續(xù)則是對(duì)結(jié)果進(jìn)行處理。
總結(jié)
Mybatis 通過 MapperProxy 代理了我們的 Dao 接口類,以此來幫助我們執(zhí)行預(yù)定義的 Sql 語句,通過 Cache 來緩存對(duì)應(yīng)的執(zhí)行結(jié)果,通過 StatementHandler 創(chuàng)建 PrepareStatement 對(duì)象,通過 jdbc 執(zhí)行 SQL 操作。
到此這篇關(guān)于淺談MyBatis 如何執(zhí)行一條 SQL語句的文章就介紹到這了,更多相關(guān)MyBatis 執(zhí)行SQL語句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決IDEA報(bào)錯(cuò)war?exploded?is?not?valid問題
在使用IntelliJ?IDEA時(shí)遇到'[projectname]warexploded'無效的問題,可以通過清除項(xiàng)目列表、重新導(dǎo)入項(xiàng)目和配置新的Tomcat來解決,確保在Tomcat配置中,將ApplicationContext修改為僅包含一個(gè)'/',這一方法或許能幫助遇到相似問題的開發(fā)者2024-09-09
Java多線程之定時(shí)器Timer的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java多線程中定時(shí)器Timer類的使用以及模擬實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
Java使用Semaphore對(duì)單接口進(jìn)行限流
本篇主要講如何使用Semaphore對(duì)單接口進(jìn)行限流,主要有三種方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)開發(fā)醫(yī)院科室及排班的接口
這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)開發(fā)醫(yī)院科室及排班的接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-04-04
SpringBoot3實(shí)現(xiàn)webclient的通用方法詳解
Spring Boot WebClient 是 Spring Framework 5 中引入的一個(gè)新的響應(yīng)式 Web 客戶端,用于異步和響應(yīng)式地與外部服務(wù)進(jìn)行通信,下面我們就來看看SpringBoot3實(shí)現(xiàn)webclient的通用方法吧2024-04-04
Java中的==和equals()區(qū)別小結(jié)
在Java編程中,理解==操作符和equals()方法的區(qū)別是至關(guān)重要的,本文主要介紹了Java中的==和equals()區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before方式
這篇文章主要介紹了SpringAop @Aspect織入不生效,不執(zhí)行前置增強(qiáng)織入@Before方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

