Mybatis執(zhí)行SQL命令的流程分析
Mybatis中的Sql命令,在枚舉類SqlCommandType中定義的。
public enum SqlCommandType { UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH; }
下面,我們以Mapper接口中的一個方法作為例子,看看Sql命令的執(zhí)行完整流程。
public interface StudentMapper { List<Student> findAllStudents(Map<String, Object> map, RowBounds rowBounds, ResultSetHandler rh); }
參數(shù)RowBounds和ResultSetHandler是可選參數(shù),表示分頁對象和自定義結(jié)果集處理器,一般不需要。
一個完整的Sql命令,其執(zhí)行的完整流程圖如下:
對于上面的流程圖,如果看過前面的文章的話,大部分對象我們都比較熟悉了。一個圖,就完整展示了其執(zhí)行流程。
MapperProxy的功能:
1. 因為Mapper接口不能直接實(shí)例化,MapperProxy的作用,就是使用JDK動態(tài)代理功能,間接實(shí)例化Mapper的proxy對象??蓞⒖聪盗械牡诙?。
2. 緩存MapperMethod對象。
private final Map<Method, MapperMethod> methodCache; @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (Object.class.equals(method.getDeclaringClass())) { try { return method.invoke(this, args); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } // 投鞭斷流 final MapperMethod mapperMethod = cachedMapperMethod(method); return mapperMethod.execute(sqlSession, args); } // 緩存MapperMethod private MapperMethod cachedMapperMethod(Method method) { MapperMethod mapperMethod = methodCache.get(method); if (mapperMethod == null) { mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()); methodCache.put(method, mapperMethod); } return mapperMethod; }
MapperMethod的功能:
1. 解析Mapper接口的方法,并封裝成MapperMethod對象。
2. 將Sql命令,正確路由到恰當(dāng)?shù)腟qlSession的方法上。
public class MapperMethod { // 保存了Sql命令的類型和鍵id private final SqlCommand command; // 保存了Mapper接口方法的解析信息 private final MethodSignature method; public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) { this.command = new SqlCommand(config, mapperInterface, method); this.method = new MethodSignature(config, method); } // 根據(jù)解析結(jié)果,路由到恰當(dāng)?shù)腟qlSession方法上 public Object execute(SqlSession sqlSession, Object[] args) { Object result; if (SqlCommandType.INSERT == command.getType()) { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.insert(command.getName(), param)); } else if (SqlCommandType.UPDATE == command.getType()) { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.update(command.getName(), param)); } else if (SqlCommandType.DELETE == command.getType()) { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.delete(command.getName(), param)); } else if (SqlCommandType.SELECT == command.getType()) { 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 { Object param = method.convertArgsToSqlCommandParam(args); result = sqlSession.selectOne(command.getName(), param); } } else if (SqlCommandType.FLUSH == command.getType()) { result = sqlSession.flushStatements(); } else { throw new BindingException("Unknown execution method for: " + command.getName()); } if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) { throw new BindingException("Mapper method '" + command.getName() + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ")."); } return result; }
org.apache.ibatis.binding.MapperMethod.SqlCommand。
public static class SqlCommand { // full id, 通過它可以找到MappedStatement private final String name; private final SqlCommandType type;
org.apache.ibatis.binding.MapperMethod.MethodSignature:
public static class MethodSignature { private final boolean returnsMany; private final boolean returnsMap; private final boolean returnsVoid; private final Class<?> returnType; private final String mapKey; private final Integer resultHandlerIndex; private final Integer rowBoundsIndex; private final SortedMap<Integer, String> params; private final boolean hasNamedParameters; public MethodSignature(Configuration configuration, Method method) { this.returnType = method.getReturnType(); this.returnsVoid = void.class.equals(this.returnType); this.returnsMany = (configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray()); this.mapKey = getMapKey(method); this.returnsMap = (this.mapKey != null); this.hasNamedParameters = hasNamedParams(method); // 分頁參數(shù) this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class); // 自定義ResultHandler this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters)); }
以上是對MapperMethod的補(bǔ)充說明。
到此這篇關(guān)于Mybatis執(zhí)行SQL命令的完整流程的文章就介紹到這了,更多相關(guān)Mybatis執(zhí)行SQL命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳談java中File類getPath()、getAbsolutePath()、getCanonical的區(qū)別
下面小編就為大家?guī)硪黄斦刯ava中File類getPath()、getAbsolutePath()、getCanonical的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07詳解Java虛擬機(jī)30個常用知識點(diǎn)之1——類文件結(jié)構(gòu)
這篇文章主要介紹了Java虛擬機(jī)類文件結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Javaweb應(yīng)用使用限流處理大量的并發(fā)請求詳解
這篇文章主要介紹了Javaweb應(yīng)用使用限流處理大量的并發(fā)請求詳解,還是挺不錯的,這里分享給大家,供需要的朋友參考。2017-11-11SpringBoot整合mybatis/mybatis-plus實(shí)現(xiàn)數(shù)據(jù)持久化的操作
這篇文章主要介紹了SpringBoot整合mybatis/mybatis-plus實(shí)現(xiàn)數(shù)據(jù)持久化,本節(jié)內(nèi)容我們介紹了數(shù)據(jù)持久化的相關(guān)操作,并且是基礎(chǔ)傳統(tǒng)的關(guān)系型數(shù)據(jù)庫——mysql,需要的朋友可以參考下2022-10-10springboot如何去獲取前端傳遞的參數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了springboot如何去獲取前端傳遞的參數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Jmeter對響應(yīng)數(shù)據(jù)實(shí)現(xiàn)斷言代碼實(shí)例
這篇文章主要介紹了Jmeter對響應(yīng)數(shù)據(jù)實(shí)現(xiàn)斷言代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09smslib發(fā)短信實(shí)例代碼(電腦發(fā)短信)
smslib發(fā)短信實(shí)例,大家可以參考使用開發(fā)自己的程序2013-12-12