本文分享自華為雲社區《一文徹底吃透MyBatis源碼!!》,作者:冰 河。 寫在前面 隨著互聯網的發展,越來越多的公司摒棄了Hibernate,而選擇擁抱了MyBatis。而且,很多大廠在面試的時候喜歡問MyBatis底層的原理和源碼實現。總之,MyBatis幾乎成為了Java開發人員必須深入掌握的 ...
本文分享自華為雲社區《一文徹底吃透MyBatis源碼!!》,作者:冰 河。
寫在前面
隨著互聯網的發展,越來越多的公司摒棄了Hibernate,而選擇擁抱了MyBatis。而且,很多大廠在面試的時候喜歡問MyBatis底層的原理和源碼實現。總之,MyBatis幾乎成為了Java開發人員必須深入掌握的框架技術,今天,我們就一起來深入分析MyBatis源碼。文章有點長,建議先收藏後慢慢研究。整體三萬字左右,全程高能,小伙伴們可慢慢研究。
MyBatis源碼解析
大家應該都知道Mybatis源碼也是對Jbdc的再一次封裝,不管怎麼進行包裝,還是會有獲取鏈接、preparedStatement、封裝參數、執行這些步驟的。
配置解析過程
String resource = "mybatis-config.xml"; //1.讀取resources下麵的mybatis-config.xml文件 InputStream inputStream = Resources.getResourceAsStream(resource); //2.使用SqlSessionFactoryBuilder創建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //3.通過sqlSessionFactory創建SqlSession SqlSession sqlSession = sqlSessionFactory.openSession();
Resources.getResourceAsStream(resource)讀取文件
public static InputStream getResourceAsStream(String resource) throws IOException { return getResourceAsStream(null, resource); } //loader賦值為null public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException { InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader); if (in == null) { throw new IOException("Could not find resource " + resource); } return in; } //classLoader為null public InputStream getResourceAsStream(String resource, ClassLoader classLoader) { return getResourceAsStream(resource, getClassLoaders(classLoader)); } //classLoader類載入 InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) { for (ClassLoader cl : classLoader) { if (null != cl) { //載入指定路徑文件流 InputStream returnValue = cl.getResourceAsStream(resource); // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource if (null == returnValue) { returnValue = cl.getResourceAsStream("/" + resource); } if (null != returnValue) { return returnValue; } } } return null; }
總結:主要是通過ClassLoader.getResourceAsStream()方法獲取指定的classpath路徑下的Resource 。
通過SqlSessionFactoryBuilder創建SqlSessionFactory
//SqlSessionFactoryBuilder是一個建造者模式 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); public SqlSessionFactory build(InputStream inputStream) { return build(inputStream, null, null); } //XMLConfigBuilder也是建造者模式 public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { try { XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties); return build(parser.parse()); } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { inputStream.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } } //接下來進入XMLConfigBuilder構造函數 public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) { this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props); } //接下來進入this後,初始化Configuration private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { super(new Configuration()); ErrorContext.instance().resource("SQL Mapper Configuration"); this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; } //其中parser.parse()負責解析xml,build(configuration)創建SqlSessionFactory return build(parser.parse());
parser.parse()解析xml
public Configuration parse() { //判斷是否重覆解析 if (parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; //讀取配置文件一級節點configuration parseConfiguration(parser.evalNode("/configuration")); return configuration; }
private void parseConfiguration(XNode root) { try { //properties 標簽,用來配置參數信息,比如最常見的資料庫連接信息 propertiesElement(root.evalNode("properties")); Properties settings = settingsAsProperties(root.evalNode("settings")); loadCustomVfs(settings); loadCustomLogImpl(settings); //實體別名兩種方式:1.指定單個實體;2.指定包 typeAliasesElement(root.evalNode("typeAliases")); //插件 pluginElement(root.evalNode("plugins")); //用來創建對象(資料庫數據映射成java對象時) objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); reflectorFactoryElement(root.evalNode("reflectorFactory")); settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 //資料庫環境 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); //資料庫類型和Java數據類型的轉換 typeHandlerElement(root.evalNode("typeHandlers")); //這個是對資料庫增刪改查的解析 mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
總結:parseConfiguration完成的是解析configuration下的標簽
private void mapperElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { //解析<package name=""/> if ("package".equals(child.getName())) { String mapperPackage = child.getStringAttribute("name"); //包路徑存到mapperRegistry中 configuration.addMappers(mapperPackage); } else { //解析<mapper url="" class="" resource=""></mapper> String resource = child.getStringAttribute("resource"); String url = child.getStringAttribute("url"); String mapperClass = child.getStringAttribute("class"); if (resource != null && url == null && mapperClass == null) { ErrorContext.instance().resource(resource); //讀取Mapper.xml文件 InputStream inputStream = Resources.getResourceAsStream(resource); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url != null && mapperClass == null) { ErrorContext.instance().resource(url); InputStream inputStream = Resources.getUrlAsStream(url); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url == null && mapperClass != null) { Class<?> mapperInterface = Resources.classForName(mapperClass); configuration.addMapper(mapperInterface); } else { throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one."); } } } } }
總結: 通過解析configuration.xml文件,獲取其中的Environment、Setting,重要的是將下的所有解析出來之後添加到
Configuration,Configuration類似於配置中心,所有的配置信息都在這裡。
mapperParser.parse()對 Mapper 映射器的解析
public void parse() { if (!configuration.isResourceLoaded(resource)) { //解析所有的子標簽 configurationElement(parser.evalNode("/mapper")); configuration.addLoadedResource(resource); //把namespace(介面類型)和工廠類綁定起來 bindMapperForNamespace(); } parsePendingResultMaps(); parsePendingCacheRefs(); parsePendingStatements(); } //這裡面解析的是Mapper.xml的標簽 private void configurationElement(XNode context) { try { String namespace = context.getStringAttribute("namespace"); if (namespace == null || namespace.equals("")) { throw new BuilderException("Mapper's namespace cannot be empty"); } builderAssistant.setCurrentNamespace(namespace); //對其他命名空間緩存配置的引用 cacheRefElement(context.evalNode("cache-ref")); //對給定命名空間的緩存配置 cacheElement(context.evalNode("cache")); parameterMapElement(context.evalNodes("/mapper/parameterMap")); //是最複雜也是最強大的元素,用來描述如何從資料庫結果集中來載入對象 resultMapElements(context.evalNodes("/mapper/resultMap")); //可被其他語句引用的可重用語句塊 sqlElement(context.evalNodes("/mapper/sql")); //獲得MappedStatement對象(增刪改查標簽) buildStatementFromContext(context.evalNodes("select|insert|update|delete")); } catch (Exception e) { throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e); } } //獲得MappedStatement對象(增刪改查標簽) private void buildStatementFromContext(List<XNode> list) { if (configuration.getDatabaseId() != null) { buildStatementFromContext(list, configuration.getDatabaseId()); } buildStatementFromContext(list, null); } //獲得MappedStatement對象(增刪改查標簽) private void buildStatementFromContext(List<XNode> list, String requiredDatabaseId) { //迴圈增刪改查標簽 for (XNode context : list) { final XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, context, requiredDatabaseId); try { //解析insert/update/select/del中的標簽 statementParser.parseStatementNode(); } catch (IncompleteElementException e) { configuration.addIncompleteStatement(statementParser); } } } public void parseStatementNode() { //在命名空間中唯一的標識符,可以被用來引用這條語句 String id = context.getStringAttribute("id"); //資料庫廠商標識 String databaseId = context.getStringAttribute("databaseId"); if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) { return; } String nodeName = context.getNode().getNodeName(); SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH)); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; //flushCache和useCache都和二級緩存有關 //將其設置為true後,只要語句被調用,都會導致本地緩存和二級緩存被清空,預設值:false boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect); //將其設置為 true 後,將會導致本條語句的結果被二級緩存緩存起來,預設值:對 select 元素為 true boolean useCache = context.getBooleanAttribute("useCache", isSelect); boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false); // Include Fragments before parsing XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant); includeParser.applyIncludes(context.getNode()); //會傳入這條語句的參數類的完全限定名或別名 String parameterType = context.getStringAttribute("parameterType"); Class<?> parameterTypeClass = resolveClass(parameterType); String lang = context.getStringAttribute("lang"); LanguageDriver langDriver = getLanguageDriver(lang); // Parse selectKey after includes and remove them. processSelectKeyNodes(id, parameterTypeClass, langDriver); // Parse the SQL (pre: <selectKey> and <include> were parsed and removed) KeyGenerator keyGenerator; String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX; keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true); if (configuration.hasKeyGenerator(keyStatementId)) { keyGenerator = configuration.getKeyGenerator(keyStatementId); } else { keyGenerator = context.getBooleanAttribute("useGeneratedKeys", configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)) ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE; } SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass); StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString())); Integer fetchSize = context.getIntAttribute("fetchSize"); Integer timeout = context.getIntAttribute("timeout"); String parameterMap = context.getStringAttribute("parameterMap"); //從這條語句中返回的期望類型的類的完全限定名或別名 String resultType = context.getStringAttribute("resultType"); Class<?> resultTypeClass = resolveClass(resultType); //外部resultMap的命名引用 String resultMap = context.getStringAttribute("resultMap"); String resultSetType = context.getStringAttribute("resultSetType"); ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType); String keyProperty = context.getStringAttribute("keyProperty"); String keyColumn = context.getStringAttribute("keyColumn"); String resultSets = context.getStringAttribute("resultSets"); builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets); } public MappedStatement addMappedStatement( String id, SqlSource sqlSource, StatementType statementType, SqlCommandType sqlCommandType, Integer fetchSize, Integer timeout, String parameterMap, Class<?> parameterType, String resultMap, Class<?> resultType, ResultSetType resultSetType, boolean flushCache, boolean useCache, boolean resultOrdered, KeyGenerator keyGenerator, String keyProperty, String keyColumn, String databaseId, LanguageDriver lang, String resultSets) { if (unresolvedCacheRef) { throw new IncompleteElementException("Cache-ref not yet resolved"); } id = applyCurrentNamespace(id, false); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType) .resource(resource) .fetchSize(fetchSize) .timeout(timeout) .statementType(statementType) .keyGenerator(keyGenerator) .keyProperty(keyProperty) .keyColumn(keyColumn) .databaseId(databaseId) .lang(lang) .resultOrdered(resultOrdered) .resultSets(resultSets) .resultMaps(getStatementResultMaps(resultMap, resultType, id)) .resultSetType(resultSetType) .flushCacheRequired(valueOrDefault(flushCache, !isSelect)) .useCache(valueOrDefault(useCache, isSelect)) .cache(currentCache); ParameterMap statementParameterMap = getStatementParameterMap(parameterMap, parameterType, id); if (statementParameterMap != null) { statementBuilder.parameterMap(statementParameterMap); } MappedStatement statement = statementBuilder.build(); //持有在configuration中 configuration.addMappedStatement(statement); return statement; } public void addMappedStatement(MappedStatement ms){ //ms.getId = mapper.UserMapper.getUserById //ms = MappedStatement等於每一個增刪改查的標簽的里的數據 mappedStatements.put(ms.getId(), ms); } //最終存放到mappedStatements中,mappedStatements存放的是一個個的增刪改查 protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection").conflictMessageProducer((savedValue, targetValue) -> ". please check " + savedValue.getResource() + " and " + targetValue.getResource());
解析bindMapperForNamespace()方法
把 namespace(介面類型)和工廠類綁定起來
private void bindMapperForNamespace() { //當前Mapper的命名空間 String namespace = builderAssistant.getCurrentNamespace(); if (namespace != null) { Class<?> boundType = null; try { //interface mapper.UserMapper這種 boundType = Resources.classForName(namespace); } catch (ClassNotFoundException e) { } if (boundType != null) { if (!configuration.hasMapper(boundType)) { configuration.addLoadedResource("namespace:" + namespace); configuration.addMapper(boundType); } } } } public <T> void addMapper(Class<T> type) { mapperRegistry.addMapper(type); } 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 { //介面類型(key)->工廠類 knownMappers.put(type, new MapperProxyFactory<>(type)); MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type); parser.parse(); loadCompleted = true; } finally { if (!loadCompleted) { knownMappers.remove(type); } } } }
生成SqlSessionFactory對象
XMLMapperBuilder.parse()方法,是對 Mapper 映射器的解析裡面有兩個方法:
(1)configurationElement()解析所有的子標簽,最終解析Mapper.xml中的insert/update/delete/select標簽的id(全路徑)組成key和整個標簽和數據連接組成MappedStatement存放到Configuration中的 mappedStatements這個map裡面。
(2)bindMapperForNamespace()是把介面類型(interface mapper.UserMapper)和工廠類存到放MapperRegistry中的knownMappers裡面。
SqlSessionFactory的創建
public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }
直接把Configuration當做參數,直接new一個DefaultSqlSessionFactory。
SqlSession會話的創建過程
mybatis操作的時候跟資料庫的每一次連接,都需要創建一個會話,我們用openSession()方法來創建。這個會話裡面需要包含一個Executor用來執行 SQL。Executor又要指定事務類型和執行器的類型。
創建Transaction(兩種方式)
- 如果配置的是 JDBC,則會使用Connection 對象的 commit()、rollback()、close()管理事務。
- 如果配置成MANAGED,會把事務交給容器來管理,比如 JBOSS,Weblogic。
SqlSession sqlSession = sqlSessionFactory.openSession();
public SqlSession openSession() { //configuration中有預設賦值protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false); } <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments>
創建Executor
//ExecutorType是SIMPLE,一共有三種SIMPLE(SimpleExecutor)、REUSE(ReuseExecutor)、BATCH(BatchExecutor) private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { //xml中的development節點 final Environment environment = configuration.getEnvironment(); //type配置的是Jbdc所以生成的是JbdcTransactionFactory工廠類 final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //Jdbc生成JbdcTransactionFactory生成JbdcTransaction tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //創建CachingExecutor執行器 final Executor executor = configuration.newExecutor(tx, execType); //創建DefaultSqlSession屬性包括 Configuration、Executor對象 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(); } }
獲得Mapper對象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); public <T> T getMapper(Class<T> type) { return configuration.getMapper(type, this); }
mapperRegistry.getMapper是從MapperRegistry的knownMappers裡面取的,knownMappers裡面存的是介面類型(interface mapper.UserMapper)和工廠類(MapperProxyFactory)。
public <T> T getMapper(Class<T> type, SqlSession sqlSession) { return mapperRegistry.getMapper(type, sqlSession); }
從knownMappers的Map里根據介面類型(interface mapper.UserMapper)取出對應的工廠類。
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 newInstance(SqlSession sqlSession) { final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); }
這裡通過JDK動態代理返回代理對象MapperProxy(org.apache.ibatis.binding.MapperProxy@6b2ea799)
protected T newInstance(MapperProxy<T> mapperProxy) { //mapperInterface是interface mapper.UserMapper return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); } UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
執行SQL
User user = userMapper.getUserById(1);
調用invoke代理方法
由於所有的 Mapper 都是 MapperProxy 代理對象,所以任意的方法都是執行MapperProxy 的invoke()方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { //判斷是否需要去執行SQL還是直接執行方法 if (Object.class.equals(method.getDeclaringClass())) { return method.invoke(this, args); //這裡判斷的是介面中的預設方法Default等 } else if (isDefaultMethod(method)) { return invokeDefaultMethod(proxy, method, args); } } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } //獲取緩存,保存了方法簽名和介面方法的關係 final MapperMethod mapperMethod = cachedMapperMethod(method); return mapperMethod.execute(sqlSession, args); }
調用execute方法
這裡使用的例子用的是查詢所以走的是else分支語句。
public Object execute(SqlSession sqlSession, Object[] args) { Object result; //根據命令類型走不行的操作command.getType()是select 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));