Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的
從源碼來分析,通過Mybatis的都知道,必須指定nameSpace為Mapper的全限定類名。這樣就能關(guān)聯(lián)起來。Mapper的實(shí)現(xiàn)肯定是動(dòng)態(tài)dialing,在InvocationHandler
中做增強(qiáng)。
這里就來分析分析具體是怎么做的?在下面分析的時(shí)候,源碼看起來比較枯燥,并且涉及到的東西很多。
分析的時(shí)候設(shè)計(jì)的東西多,容易走偏。我盡量回歸主題。
1. XML文件解析
這里的xml解析比較繁瑣,如果逐行來分析的話,很多很多,這里就挑主線來分析了。之后會(huì)分塊來分話題來做分析。
解析總的配置文件
如果從經(jīng)典的Mybatis創(chuàng)建SqlSessionFactory
開始,那肯定能看到下面的代碼
代碼里面的有的注釋,是我看源碼的時(shí)候?qū)懙模械膶懙谋容^離譜。有的記錄我之前看的時(shí)候的困惑。之后看的時(shí)候又看懂了。所以就保留在這里了。
private void parseConfiguration(XNode root) { try { // issue #117 read properties first propertiesElement(root.evalNode("properties")); //解析properties標(biāo)簽,并把他放在 parser和config的 Variables 里面 Properties settings = settingsAsProperties(root.evalNode("settings"));//加載setting標(biāo)簽 loadCustomVfs(settings); //lcnote 這里的vfs是啥?怎么用 我知道這個(gè) //我現(xiàn)在知道他的vfs是什么了,vfs(virtual file system)他抽象出了幾個(gè)api,通過這些api就可以訪問文件系統(tǒng)上的資源;比如在 // 在解析 mapperElement(root.evalNode("mappers"));的時(shí)候,如果指定package,就可以通過VFS來獲取包路徑下面所有的class文件。 // 并且會(huì)將他添加到mappe里面,和spring中的classPathSacnner一樣差不多,可以指定過濾器。 loadCustomLogImpl(settings); typeAliasesElement(root.evalNode("typeAliases")); //從這里開始,都是解析具體的標(biāo)簽,new出對(duì)象,將標(biāo)簽下面的屬性設(shè)置進(jìn)去, // 從解析的這里基本也能看出mybatis里面重要的幾個(gè)點(diǎn),首先是objectFactory,objectFactory。objectFactory,plugins pluginElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectFactory")); reflectorFactoryElement(root.evalNode("objectFactory")); //這里就具體設(shè)置setting標(biāo)簽了 settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); typeHandlerElement(root.evalNode("typeHandlers")); //lcnote 這里是重點(diǎn),解析mapper文件, mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } }
直接看是怎么解析mapper文件的。
從這個(gè)代碼里面可以看到,mappers
標(biāo)簽下面是可以寫兩種標(biāo)簽。package
和mapper
標(biāo)簽。對(duì)于兩種有不同的解析方法。
解析package標(biāo)簽
這里只是截取了部分的源碼。還會(huì)將好幾個(gè)源碼都拼接在一塊,便于看
if ("package".equals(child.getName())) { String mapperPackage = child.getStringAttribute("name"); configuration.addMappers(mapperPackage); } //下面是 configuration.addMappers(mapperPackage)的方法 public void addMappers(String packageName) { //mapperRegistry是一個(gè)注冊(cè)mapper的注冊(cè)器,并且里面維護(hù)了很多的所有的mapper組成的對(duì)象。 mapperRegistry.addMappers(packageName); } //下面是mapperRegistry.addMappers(packageName); public void addMappers(String packageName) { addMappers(packageName, Object.class); } // addMappers(packageName, Object.class); 方法, //packageName表示要掃描的包的路徑 //superType表示要找的類是這個(gè)類的子類。 public void addMappers(String packageName, Class<?> superType) { //resolverUtil就是一個(gè)在指定包下,找指定的類的子類集合的一個(gè)工具類。 ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>(); resolverUtil.find(new ResolverUtil.IsA(superType), packageName); //找到了合適的Class,將他添加到mapper里面。 Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses(); for (Class<?> mapperClass : mapperSet) { addMapper(mapperClass); } } //resolverUtil.find(new ResolverUtil.IsA(superType), packageName); //掃描給定包下(包括子路徑下面的所有的類。調(diào)用Test方法來匹配,匹配到的class調(diào)用getClasses就可以獲取的到。) public ResolverUtil<T> find(Test test, String packageName) { String path = getPackagePath(packageName); try { List<String> children = VFS.getInstance().list(path); for (String child : children) { if (child.endsWith(".class")) { //加載class對(duì)象,調(diào)用ResolverUtil里面的靜態(tài)內(nèi)部類IsA(實(shí)現(xiàn)了Test接口)做匹配。 addIfMatching(test, child); } } } catch (IOException ioe) { log.error("Could not read package: " + packageName, ioe); } return this; } //addIfMatching(test, child); protected void addIfMatching(Test test, String fqn) { try { String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.'); ClassLoader loader = getClassLoader(); if (log.isDebugEnabled()) { log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]"); } Class<?> type = loader.loadClass(externalName); if (test.matches(type)) { matches.add((Class<T>) type); } } catch (Throwable t) { log.warn("Could not examine class '" + fqn + "'" + " due to a " + t.getClass().getName() + " with message: " + t.getMessage()); } } //****************************重點(diǎn)*********************************** // public <T> void addMapper(Class<T> type) 方法,將上面找的,合適的class實(shí)例化之后要加載到mapperRegistry里面去。 // 并且這個(gè)方法是mapperRegistry里面的。 public <T> void addMapper(Class<T> type) { if (type.isInterface()) { if (hasMapper(type)) {//mapper只能注冊(cè)一次 throw new BindingException("Type " + type + " is already known to the MapperRegistry."); } boolean loadCompleted = false; try { // 用mapper new出MapperProxyFactory,放在knownMappers里面 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. // 這個(gè)是很重要的,在解析之前的添加類型,因此,他會(huì)自動(dòng)嘗試綁定解析mapper。如果類型知道,沒啥事, // 這里我覺得是解析mapper里面的注解。 MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type); parser.parse(); loadCompleted = true; } finally { if (!loadCompleted) { knownMappers.remove(type); } } } }
這里面出現(xiàn)的幾個(gè)重要的類
MapperRegistry
: mapper的注冊(cè)器,保存所有的mapper。Configuration
:Configuration對(duì)象報(bào)錯(cuò)了Mybatis運(yùn)行期間能用到的所有的數(shù)據(jù)。MapperProxyFactory
:代理mapper的創(chuàng)建工廠,這里面沒有啥特殊的,就是調(diào)用創(chuàng)建代理對(duì)象的方式來創(chuàng)建對(duì)象。MapperAnnotationBuilder
:解析mapper里面的注解。這些注解和XMl的功能是一樣的,但是不推薦使用。
解析mapper標(biāo)簽
看源碼的時(shí)候有這種感覺,哇哦,這居然可以這樣用,這個(gè)框架居然還有這種功能。
{ 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); //resource和url的加載操作是一直的,就是resource的來源不一樣。 // 這里就會(huì)加載resource,解析mapper文件,構(gòu)建mapperStatement對(duì)象, try(InputStream inputStream = Resources.getResourceAsStream(resource)) { XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse();//lcnote 這里的解析操作和配置文件解析操作是一樣的。都是構(gòu)建XMLMapperBuilder,然后調(diào)用parse方法 } } else if (resource == null && url != null && mapperClass == null) { ErrorContext.instance().resource(url); try(InputStream inputStream = Resources.getUrlAsStream(url)){ XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments()); mapperParser.parse(); } } else if (resource == null && url == null && mapperClass != null) { //這里沒有什么特殊,就是什么解析package標(biāo)簽,得到mapper之后加載的過程, 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."); } }
從這里可以看到,支持三種屬性,resource,url和class,并且加載的順序也是resource優(yōu)先,url和class,并且三個(gè)不能同時(shí)指定。
從上面可以看出,resource和url的加載操作是一致的,就是resource的來源不一樣。
class的加載和解析package標(biāo)簽,得到mapper之后加載的過程,是一致的,這里就直接看
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
開始了
構(gòu)建XMLMapperBuilder
這里要說說BaseBuilder
,這個(gè)類在Mybatis中是很基礎(chǔ)的類。好多解析都是繼承與他,才開始做解析的。
還有一點(diǎn)點(diǎn)的說明,MapperBuilderAssistant確實(shí)是一個(gè)工具類,先看看他的構(gòu)造
public class MapperBuilderAssistant extends BaseBuilder { private String currentNamespace; // 當(dāng)前解析的nameSpace private final String resource; // 當(dāng)前nameSpace對(duì)應(yīng)的resource文件 private Cache currentCache; // 當(dāng)前的緩存,對(duì)應(yīng)的mapper標(biāo)簽里面的cache標(biāo)簽。 private boolean unresolvedCacheRef; // issue #676 }
這個(gè)類對(duì)應(yīng)的就是一個(gè)mapper文件解析時(shí)候產(chǎn)生的所有的東西。比如resultMap,sql,select,update,等等。這些相關(guān)的東西。都會(huì)通過這個(gè)對(duì)象添加到BaseBuilder里面去。
XMLMapperBuilder
繼承與BaseBuilder,XMLMapperBuilder
主要是用來解析配置文件中的mappers
中的mapper
標(biāo)簽。
// 看看構(gòu)造類 public XMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource, Map<String, XNode> sqlFragments) { this(new XPathParser(inputStream, true, configuration.getVariables(), new XMLMapperEntityResolver()), configuration, resource, sqlFragments); } private XMLMapperBuilder(XPathParser parser, Configuration configuration, String resource, Map<String, XNode> sqlFragments) { super(configuration); this.builderAssistant = new MapperBuilderAssistant(configuration, resource); this.parser = parser; this.sqlFragments = sqlFragments; this.resource = resource; } //super的構(gòu)造方法 public BaseBuilder(Configuration configuration) { this.configuration = configuration; this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();//從配置文件中獲取 typeAliases標(biāo)簽相關(guān)內(nèi)容 this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();//從配置文件中獲取typeHandlers } 構(gòu)造函數(shù)沒有什么可說的,重點(diǎn)是下面的Parse方法
上面出現(xiàn)的幾個(gè)重要的類
- XMLMapperEntityResolver :xmlMapper的實(shí)體解析器。繼承與
EntityResolver
。它是org.xml.sax包中的對(duì)象。 - BaseBuilder:所有xml解析的基礎(chǔ)類。
調(diào)用XMLMapperBuilder的parse方法
public void parse() { //configuration里面保存了加載過的resource集合,這里先判斷一下 if (!configuration.isResourceLoaded(resource)) { //會(huì)去configuration里面的一個(gè)set里面去查找 // 這里是重點(diǎn),重點(diǎn)就是解析mapper標(biāo)簽 configurationElement(parser.evalNode("/mapper"));//解析mapper標(biāo)簽 configuration.addLoadedResource(resource);//添加到已經(jīng)加載過的集合中 bindMapperForNamespace(); //嘗試通過nameSpace來加載配置文件。 //注意,這里說的是嘗試,nameSpace并不必須和Mapper接口保持一致。 } //下面的操作也很有意思。 //解析xml的時(shí)候,如果報(bào)錯(cuò)(IncompleteElementException)不會(huì)立即拋出,而是會(huì)將這些報(bào)錯(cuò)的緩存起來,在上面的都解析完成之后,在嘗試一下。 // parsePendingResultMaps(); parsePendingCacheRefs(); parsePendingStatements(); }
這里主要就是解析mapper
標(biāo)簽,并且嘗試
通過nameSpace來加載對(duì)應(yīng)的mapper。如果加載到了,就會(huì)調(diào)用上面的MapperRegistry
將mapper注冊(cè)到里面。
這里的解析操作和之前解析configuration
標(biāo)簽的操作很類似,先解析父標(biāo)簽在解析子標(biāo)簽。下面看看具體是怎么解析的
private void configurationElement(XNode context) { try { // 解析namespace String namespace = context.getStringAttribute("namespace"); if (namespace == null || namespace.isEmpty()) { throw new BuilderException("Mapper's namespace cannot be empty"); } builderAssistant.setCurrentNamespace(namespace); cacheRefElement(context.evalNode("cache-ref"));//解析各個(gè)標(biāo)簽元素 // 解析cache標(biāo)簽 cacheElement(context.evalNode("cache")); parameterMapElement(context.evalNodes("/mapper/parameterMap")); resultMapElements(context.evalNodes("/mapper/resultMap")); //解析sql sqlElement(context.evalNodes("/mapper/sql")); //waring 這里很重要,真正的開始解析select|insert|update|delete標(biāo)簽 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); } }
說明
1.解析parameterMap,在經(jīng)過上面的解析之后,構(gòu)建 ParameterMapping,添加到builderAssistant里面。在builderAssistant里面會(huì)轉(zhuǎn)換成 ParameterMap,最后添加到 Configuration對(duì)象parameterMaps屬性里面。這個(gè)Configuration就是全局通用。并且一個(gè)mapper里面能有多個(gè)parameterMap標(biāo)簽。
2.解析cache,得到對(duì)應(yīng)的屬性元素的值,構(gòu)建Cache對(duì)象,添加到configuration里面,將builderAssistant中的currentCache賦值為當(dāng)前的cache對(duì)象。并且一個(gè)Mapper只能有一個(gè)cache標(biāo)簽。
3.解析resultMap,這里的解析相比前面兩個(gè)就比較復(fù)雜了,resultMap下面有很多標(biāo)簽。
for (XNode resultChild : resultChildren) { if ("constructor".equals(resultChild.getName())) { //這個(gè)很簡(jiǎn)單了,通過構(gòu)造方法來設(shè)置參數(shù) processConstructorElement(resultChild, typeClass, resultMappings); } else if ("discriminator".equals(resultChild.getName())) { // lcnote 對(duì)應(yīng)的Discriminator對(duì)象,現(xiàn)實(shí)中Discriminator標(biāo)簽沒有用過,之后看看,看起來這個(gè)標(biāo)簽?zāi)軐?shí)現(xiàn)swtich case的功能,而且還可以搭配resultMap // 來做一些有趣的事情。之前這個(gè)確實(shí)么有用過 discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings); } else { List<ResultFlag> flags = new ArrayList<>(); if ("id".equals(resultChild.getName())) { flags.add(ResultFlag.ID); } resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags)); } }
- 對(duì)于constructor標(biāo)簽,構(gòu)建ResultMapping對(duì)象添加到ResultMapping集合中。
- 對(duì)于discriminator標(biāo)簽 我沒用過,之后在寫寫吧。構(gòu)建Discriminator對(duì)象。
- 對(duì)于別的標(biāo)簽,構(gòu)建ResultMapping對(duì)象添加到ResultMapping集合中。
這都是一個(gè)resultMap標(biāo)簽下面的東西,在解析完一個(gè)resultMap之后,會(huì)將上面相關(guān)的對(duì)象組裝成ResultMapResolver,調(diào)用resultMapResolver.resolve();方法,構(gòu)建成ResultMap對(duì)象,放在Configuration中。所以ResultMap就是resultmap標(biāo)簽對(duì)應(yīng)的實(shí)體類
4.解析sql標(biāo)簽。將xnode和id(sql標(biāo)簽指定的id)放在XMLMapperBuilder對(duì)象的sqlFragments中,sqlFragments是一個(gè)StrictMap繼承與HashMap,重寫了里面的put,和get方法,主要是在put和get的時(shí)候增加了判斷。sqlFragments存放的是sql片段,注意,解析這里的時(shí)候并沒有處理sql里面的動(dòng)態(tài)標(biāo)簽的部分。要知道動(dòng)態(tài)標(biāo)簽是隨著參數(shù)來確定的。這里只是一個(gè)簡(jiǎn)單的把他存起來了。并且sql標(biāo)簽是多個(gè)。
5.解析select|insert|update|delete標(biāo)簽。這是重點(diǎn)。為了清楚,還是對(duì)著源碼來看吧,select|insert|update|delete標(biāo)簽是多個(gè)。所以這里是循環(huán)解析,下面的代碼只是循環(huán)體里面的解析操作。
public void parseStatementNode() { String id = context.getStringAttribute("id"); String databaseId = context.getStringAttribute("databaseId"); //知道的,在mybatis里面是可以指定dataBase的,并且也可以在標(biāo)簽里面指定要應(yīng)用的databaseid。 // 這里就是一個(gè)判斷,如果不是當(dāng)前要應(yīng)用的,就不會(huì)解析。。 if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) { return; } String nodeName = context.getNode().getNodeName(); // 通過標(biāo)簽的名字來判斷sql的類型。 SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH)); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; //如果沒有指定flushCache,并且是select類型,默認(rèn)是false。 boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect); //如果沒有指定 useCache ,并且是select類型,默認(rèn)是true。 boolean useCache = context.getBooleanAttribute("useCache", isSelect); // 這個(gè)標(biāo)簽是啥意思,我沒用過。這種還是建議看看mybatis的官方文檔。 boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false); // Include Fragments before parsing // 在解析sql之前,想將includ標(biāo)簽解析??催@個(gè)名字也能看得出來。這就是用來處理<include>標(biāo)簽的。 // 這也就解釋了,之前在解析sql標(biāo)簽的時(shí)候?yàn)樯哆@么簡(jiǎn)單了。sql標(biāo)簽最終是要用在 Statement里面的。 // 在Statement里面也是要寫動(dòng)態(tài)sql的,所以,在真正開始解析標(biāo)簽之前,就先把他包含進(jìn)來。一塊放在 // 后面的解析操作里面。一塊解析 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. // 解析selectkey processSelectKeyNodes(id, parameterTypeClass, langDriver); //lcnote 解析sql selectKey在解析之前已經(jīng)remove掉了 // Parse the SQL (pre: <selectKey> and <include> were parsed and removed) //這里判斷是否需要使用useGeneratedKeys,這里還維護(hù)了一個(gè)緩存??梢钥纯矗琲d就是selcet標(biāo)簽的id KeyGenerator keyGenerator; String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX; keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true); // eg:org.apache.ibatis.domain.mybatis.mapper.StudentMapper.listAllStudent!selectKey if (configuration.hasKeyGenerator(keyStatementId)) { keyGenerator = configuration.getKeyGenerator(keyStatementId); } else { keyGenerator = context.getBooleanAttribute("useGeneratedKeys", configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)) ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE; } // 說實(shí)話,Mybatis的langDriver我還真不知道是什么,之后在分析分析 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); String resultMap = context.getStringAttribute("resultMap"); String resultSetType = context.getStringAttribute("resultSetType"); ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType); if (resultSetTypeEnum == null) { resultSetTypeEnum = configuration.getDefaultResultSetType(); } String keyProperty = context.getStringAttribute("keyProperty"); String keyColumn = context.getStringAttribute("keyColumn"); String resultSets = context.getStringAttribute("resultSets"); //看到這里就知道,肯定是通過builderAssistant,將組裝好的MappedStatement添加到 // configuration里面維護(hù)了statement的map,key就是namespace+mapper的id、 builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets); }
調(diào)用XMLMapperBuilder的parse方法,會(huì)解析Mapper.xml文件
- 首先要知道,每一個(gè)xml元素組成的數(shù)據(jù)范圍,在Mybatis中肯定是要有一個(gè)對(duì)應(yīng)的對(duì)象的。
- 在處理xml的時(shí)候,肯定要用到別名和類型處理器。這倆是通用的。之后還會(huì)見到。
- 將解析好的對(duì)象都要添加到configuration里面。
- 在解析mapper文件的時(shí)候還是比較有意思的。比如,在sql標(biāo)簽里面可以用${}來引用環(huán)境變量。還支持嵌套引用。
- 確實(shí),在看源碼的時(shí)候發(fā)現(xiàn)居然有這種用法,有的東西沒有用過。之后在詳細(xì)的看看吧
- 在解析的時(shí)候如果報(bào)錯(cuò)了,不會(huì)立即拋出。而是把他放在一個(gè)集合里面,等所有的xml文件解析完成了,在嘗試解析一下。
嘗試通過nameSpace綁定mapper
private void bindMapperForNamespace() { // 前面說過,builderAssistant對(duì)應(yīng)的是一個(gè)mapper解析期間的工具類。拿到namespace String namespace = builderAssistant.getCurrentNamespace(); if (namespace != null) { Class<?> boundType = null; try { //嘗試通過全限定類名加載 boundType = Resources.classForName(namespace); } catch (ClassNotFoundException e) { // ignore, bound type is not required } if (boundType != null && !configuration.hasMapper(boundType)) { // Spring may not know the real resource name so we set a flag // to prevent loading again this resource from the mapper interface // look at MapperAnnotationBuilder#loadXmlResource configuration.addLoadedResource("namespace:" + namespace); configuration.addMapper(boundType); } } }
到這里就很明確了,在解析xml文件的時(shí)候會(huì)生成對(duì)應(yīng)的標(biāo)簽,然后將它們添加到configuration里面,然后通過nameSpace加載class類,如果nameSpace要和Mapper對(duì)應(yīng)起來,還是必須要一樣的,如果不需要對(duì)應(yīng)的話,那沒事了, 隨便寫。
將加載到的class添加到configuration里面。configuration里面維護(hù)著一個(gè)map,key是class,value是MapperProxyFactory。 要注意 configuration.addMapper(boundType);方法。下面我們會(huì)看看這個(gè)方法。
通過mapper標(biāo)簽里面的nameSpace做緩存。并且生成代理對(duì)象創(chuàng)建工廠。
這個(gè)方法是MapperRegistry里面的。
public <T> void addMapper(Class<T> type) { if (type.isInterface()) { if (hasMapper(type)) {//mapper只能注冊(cè)一次 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. // 這個(gè)是很重要的,在解析之前的添加類型,因此,他會(huì)自動(dòng)嘗試綁定解析mapper。如果類型知道,沒啥事, // 解析mapper里面的注解。 MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type); parser.parse(); loadCompleted = true; } finally { if (!loadCompleted) { knownMappers.remove(type); } } } }
重點(diǎn)是MapperProxyFactory
/** * @author Lasse Voss * lcnote mapper代理對(duì)象的創(chuàng)建工廠 */ public class MapperProxyFactory<T> { // 需要代理的接口,也就是mapper private final Class<T> mapperInterface; //保存的緩存,避免new處重復(fù)對(duì)象。 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; } // 這個(gè)new操作,沒有啥特殊的,就簡(jiǎn)單的調(diào)用創(chuàng)建代理對(duì)象的方法來創(chuàng)建。 @SuppressWarnings("unchecked") 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); } }
2. Mapper動(dòng)態(tài)代理的創(chuàng)建
添加的 InvocationHandler長(zhǎng)什么樣子?
下面就是重點(diǎn)中的重點(diǎn) MapperProxy
,這里的太長(zhǎng)了,我就挑重要的講了,MapperProxy實(shí)現(xiàn)了InvocationHandler,那肯定是動(dòng)態(tài)代理。mapper肯定是利用反射來和xml文件關(guān)聯(lián)的。
下面的這個(gè)方法不是在new MapperProxyFactory時(shí)候調(diào)用的,在調(diào)用的時(shí)候會(huì)通過MapperProxyFactory創(chuàng)建出來,這里就順著上面的看下來了。
在方法調(diào)用的時(shí)候,對(duì)于default的方法,將方法封裝成MapperMethod
,然后再用 PlainMethodInvoker
包裝調(diào)用。
/** waring, 這也是比較重要的,在mapper調(diào)用的時(shí)候?qū)崿F(xiàn)的InvocationHandler * @author Clinton Begin * @author Eduardo Macarron */ public class MapperProxy<T> implements InvocationHandler, Serializable { private static final long serialVersionUID = -4724728412955527868L; private static final int ALLOWED_MODES = MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC; private static final Constructor<Lookup> lookupConstructor; private final SqlSession sqlSession; private final Class<T> mapperInterface; private final Map<Method, MapperMethodInvoker> methodCache; @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (Object.class.equals(method.getDeclaringClass())) {//如果是object類里面的方法,直接調(diào)用就好了 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 { try { return MapUtil.computeIfAbsent(methodCache, method, m -> { if (m.isDefault()) {//如果接口里面的方法是default的 try { if (privateLookupInMethod == null) { return new DefaultMethodInvoker(getMethodHandleJava8(method)); } else { return new DefaultMethodInvoker(getMethodHandleJava9(method)); } } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { throw new RuntimeException(e); } } else { //lcnote MapperMethod代表一個(gè)mapper方法。里面包括方法對(duì)應(yīng)的dataId,還有對(duì)應(yīng)的sql類型,還有方法的具體的簽名信息,包括方法返回值,param參數(shù)。mapkey注解 return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration())); } }); } catch (RuntimeException re) { Throwable cause = re.getCause(); throw cause == null ? re : cause; } } // mapper方法調(diào)用接口, interface MapperMethodInvoker { Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable; } private static class PlainMethodInvoker implements MapperMethodInvoker { private final MapperMethod mapperMethod; // 這只是實(shí)現(xiàn)mapper調(diào)用的工具類而已. 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); } } }
什么時(shí)候創(chuàng)建對(duì)象?
從SqlSession的getMapper方法開始。從這里就開始創(chuàng)建代理對(duì)象了。
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 { // 調(diào)用MapperProxyFactory來創(chuàng)建mapper實(shí)例。 return mapperProxyFactory.newInstance(sqlSession); } catch (Exception e) { throw new BindingException("Error getting mapper instance. Cause: " + e, e); } }
newInstance就會(huì)創(chuàng)建出MapperProxy
對(duì)象。MapperProxy
對(duì)象在上面介紹了。
可見,這里是利用動(dòng)態(tài)代理來創(chuàng)建了一個(gè)MapperProxy
對(duì)象。
MapperProxy里面有什么?
SqlCommand
表示關(guān)聯(lián)的mapper和這方法關(guān)聯(lián)的sql的類型
會(huì)通過Mapper的全限定類名從Configuration里面找出MapperStatement。賦值id。
判斷此方法是那種sql類型。
MethodSignature
表示一個(gè)方法的主要信息。
private final boolean returnsMany; private final boolean returnsMap; //返回值是不是一個(gè)map,只要mapKey不為null,這就是true private final boolean returnsVoid; //標(biāo)志位,是否沒有返回值 private final boolean returnsCursor; //是否返回了一個(gè)Cursor private final boolean returnsOptional; private final Class<?> returnType; //這個(gè)方法真正返回的類型,比如List<Student>真實(shí)返回的類型就是List private final String mapKey; //mapkey就是MapKey注解里面的value,關(guān)于這個(gè)MapKey的作用,之后可以寫一篇文章來分析分析 private final Integer resultHandlerIndex; private final Integer rowBoundsIndex; private final ParamNameResolver paramNameResolver;
MapperMethod
這對(duì)象里面包含了上面兩個(gè)對(duì)象,在真正執(zhí)行的時(shí)候,會(huì)調(diào)用execute
方法。
Mapper和XML就關(guān)聯(lián)起來了,在往下面就要執(zhí)行sql了,下面的步驟肯定有通過全限定類名找到MapperStatement,處理入?yún)?,處理?dòng)態(tài)sql。這里會(huì)調(diào)用OGNL來解析。然后執(zhí)行并且處理結(jié)果。后面的流程就之后在說。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
RestTemplate在Spring或非Spring環(huán)境下使用精講
這篇文章主要為大家介紹了RestTemplate在Spring或非Spring環(huán)境下使用精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Spring Security之默認(rèn)的過濾器鏈及自定義Filter操作
這篇文章主要介紹了Spring Security之默認(rèn)的過濾器鏈及自定義Filter操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06利用Spring Social輕松搞定微信授權(quán)登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12springboot如何獲取相對(duì)路徑文件夾下靜態(tài)資源的方法
這篇文章主要介紹了springboot如何獲取相對(duì)路徑文件夾下靜態(tài)資源的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05SpringBoot集成xxl-job實(shí)現(xiàn)超牛的定時(shí)任務(wù)的步驟詳解
XXL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),其核心設(shè)計(jì)目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡(jiǎn)單、輕量級(jí)、易擴(kuò)展,現(xiàn)已開放源代碼并接入多家公司線上產(chǎn)品線,開箱即用,本文給大家介紹了SpringBoot集成xxl-job實(shí)現(xiàn)超牛的定時(shí)任務(wù),需要的朋友可以參考下2023-10-10Springboot啟動(dòng)報(bào)錯(cuò)Input length = 2的問題解決
最近使用Springboot啟動(dòng)報(bào)錯(cuò),報(bào)錯(cuò)內(nèi)容java.nio.charset.MalformedInputException: Input length = 2,下面就來介紹一下解決方法,感興趣的可以了解一下2024-08-08