mybatis原始碼
- 1、回顧JDBC
- 1.1 jdbc執行流程
- 1.2 SqlSessionFactory & SqlSession
- 1.2.1 獲取SqlSession
- 1.2.1.1 原始碼決議
- 1.3 MapperProxy
- 1.4 Excutor
- 1.4.1 執行流程
- 1.4.2 MapperProxy
- 下期詳細講解Mybatis中用到的設計模式,以及具體實作
- 交流群 867157531
1、回顧JDBC
1.1 jdbc執行流程

實作代碼:
/*第一步,獲取連接*/
Connection connection = DriverManager.getConnection(JDBC.URL,JDBC.USER,JDBC.PASSWORD);
/*第二步,預編譯SQL*/
PreparedStatement statement = connection.prepareStatment("SELECT * FROM USER");
/*第三步,執行SQL*/
ResultSet resultSet = statement.executeQuery(statement);
/*第四步,獲取結果集*/
readResultSet(resultSet);
1.2 SqlSessionFactory & SqlSession
1.2.1 獲取SqlSession

1.2.1.1 原始碼決議
- SqlSessionFactoryBuilder.build();
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(Reader reader) {
/**呼叫內部build方法*/
return build(reader, null, null);
}
public SqlSessionFactory build(Reader reader, String environment) {
return build(reader, environment, null);
}
public SqlSessionFactory build(Reader reader, Properties properties) {
return build(reader, null, properties);
}
}
- SqlSessionFactoryBuilder內部build()一個DefaultSqlSessionFactory
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
/**決議XML決議器*/
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
/**parser.parse() 獲取Configuration,build一個DefaultSqlSessionFactory*/
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
}
- 獲取到SqlSessionFactory后通過SqlSessionFactory獲取SqlSession物件
/**SqlSessionFactory物件中的openSession方法最終都會呼叫openSessionFromDataSource方法*/
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
//通過Configuration獲取mybatis的配置資訊
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//結合JDBC的執行流程來看 與資料庫相互是statement物件,實際上executor是對于statement的封裝,也就是說executor是statement的一個執行器
final Executor executor = configuration.newExecutor(tx, execType);
// 重點!! 創建了一個DefaultSqlSession物件
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();
}
}
- 通過以上步驟,我們已經獲取到了一個SqlSession,按照JDBC的步驟來說我們應該去執行sql了,結合以下Demo理解
SqlSessionFactory sqlSessionFactory = new SqlSessionFactory();
String resource = "classpath:mybatis-config.xml"
try{
//SqlSessionFactoryBuilder讀取組態檔
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
} catch(IOException e){
e.printStackTrace();
}
//獲取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession()
1.3 MapperProxy
- 到目前為止我們寫的.mapper檔案還沒有使用!!!,下面介紹 MapperPorxy

在mybatis中我的寫的dao層的介面其實是MapperProxy在代理,也就是說我們在執行dao層中的方法是,其實是在執行MapperProxy - 我們通過SqlSession從Configuration中找到一個getMapper方法
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
// mapperRegistry是什么?見名知意 應該是mapper注冊機之類的東西,接著往下走看看這個mapperRegister是什么
return mapperRegistry.getMapper(type, sqlSession);
}
- 我們看下MapperRegistry是什么
// MapperRegistry實際上就是一個注冊機用來呼叫MapperProxyFactory工廠的
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) {
// 這里定了一個MapperProxyFactory的工廠
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
//代理工廠的實體 回傳了一個mapper的代理物件工廠的實體,這是不是就是我們想要的dao層物件呢?我們繼續往下看MapperProxyFactory
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
}
- MapperProxyFactory的作用是什么
/**部分代碼*/
public class MapperProxyFactory<T> {
private final Class<T> mapperInterface;
private final Map<Method, MapperMethodInvoker> methodCache = new ConcurrentHashMap<>();
public MapperProxyFactory(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}
public Class<T> getMapperInterface() {
return mapperInterface;
}
public Map<Method, MapperMethodInvoker> getMethodCache() {
return methodCache;
}
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
//!!!重點來了 這里代理到了我們寫的Dao層介面
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);
}
}
通過以上的動態代理就可以獲取到我們的Dao層
//獲取Dao物件
TestDao dao = sqlSession.getMapper(TestDao.class);
有個疑問,我們定義的Dao是介面(Interface),按理說介面是不能實體物件的,那我們這個物件是怎么得到的呢?
答:建議補習java的動態代理,劃重點–>Proxy.newProxyInstance() AOP也是基于動態代理實作的!
1.4 Excutor
1.4.1 執行流程
到這里我們獲取到了SqlSession 和 我們的mapper介面,那接下來應該做什么呢? 沒錯! 執行SQL ,我們去看下真正的SQL執行流程

1.4.2 MapperProxy
上面提到 我們通過MapperProxyFactory拿到了MapperProxy,我們都知道每一個MapperProxy都是對應的我們的dao層介面
//MapperProxy在執行的時候會觸發此方法
interface MapperMethodInvoker {
Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable;
}
private static class PlainMethodInvoker implements MapperMethodInvoker {
private final MapperMethod mapperMethod;
@Override
public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
// 這里交給mapperMethod去處理
return mapperMethod.execute(sqlSession, args);
}
}
//這里是對資料庫操作型別的判斷,最侄訓是回傳了SqlSession,那我們去看看SqlSession的CRUD方法
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());
}
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;
}
我們進入到SqlSession的實作類DefaultSqlSession隨便找一個方法我們進行查看 我這里選擇了SelectOne
@Override
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
// 我們點擊selectList方法一直點 我們最終可以發現
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;
}
}
我們隨著this.selectList()方法一直查看最終我們會在SimpleExecutor這個類看到這塊內容
@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);
}
}
@Override
protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
Statement stmt = prepareStatement(handler, ms.getStatementLog());
Cursor<E> cursor = handler.queryCursor(stmt);
stmt.closeOnCompletion();
return cursor;
}
@Override
public List<BatchResult> doFlushStatements(boolean isRollback) {
return Collections.emptyList();
}
//有沒有發現這里很熟悉?,這就是我們創建連接獲取Statement的操作!這下就這真相大白了!
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;
}
下期詳細講解Mybatis中用到的設計模式,以及具體實作
交流群 867157531
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/263793.html
標籤:其他
上一篇:阿里面試官:連Java核心技能都掌握不全,還好意思來面試P7?
下一篇:阿里實習面經
