源码分析Mybatis MappedStatement的创建流程

}

上文有两个入口:

代码@1:处理configLocation属性。

代码@2:处理mapperLocations属性。

我们先从XMLConfigBuilder#parse开始进行追踪。该方法主要是解析configLocation指定的配置路径,对其进行解析,具体调用parseConfiguration方法。

2.1 XMLConfigBuilder

我们直接查看其parseConfiguration方法。

private void parseConfiguration(XNode root) {

try {

propertiesElement(root.evalNode(“properties”)); //issue #117 read properties first

typeAliasesElement(root.evalNode(“typeAliases”));

pluginElement(root.evalNode(“plugins”));

objectFactoryElement(root.evalNode(“objectFactory”));

objectWrapperFactoryElement(root.evalNode(“objectWrapperFactory”));

settingsElement(root.evalNode(“settings”));

environmentsElement(root.evalNode(“environments”)); // read it after objectFactory and objectWrapperFactory issue #631

databaseIdProviderElement(root.evalNode(“databaseIdProvider”));

typeHandlerElement(root.evalNode(“typeHandlers”));

mapperElement(root.evalNode(“mappers”)); // @1

} catch (Exception e) {

throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);

}

}

重点关注mapperElement,从名称与参数即可以看出,该方法主要是处理中mappers的定义,即mapper sql语句的解析与处理。如果使用过Mapper的人应该不难知道,我们使用mapper节点,通过resource标签定义具体xml文件的位置。

2.1.1XMLConfigBuilder#mapperElement

private void mapperElement(XNode parent) throws Exception {

if (parent != null) {

for (XNode child : parent.getChildren()) {

if (“package”.equals(child.getName())) {

String mapperPackage = child.getStringAttribute(“name”);

configuration.addMappers(mapperPackage);

} else {

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);

InputStream inputStream = Resources.getResourceAsStream(resource);

XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); // @1

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.”);

}

}

}

}

}

上面的代码比较简单,不难看出,解析出Mapper标签,解析出resource标签的属性,创建对应的文件流,通过构建XMLMapperBuilder来解析对应的mapper.xml文件。此时大家会惊讶的发现,在SqlSessionFacotry的初始化代码中,处理mapperLocations时就是通过构建XMLMapperBuilder来解析mapper文件,其实也不难理解,因为这是mybatis支持的两个地方可以使用mapper标签来定义mapper映射文件,具体解析代码当然是一样的逻辑。那我们解析来重点把目光投向XMLMapperBuilder。

2.2 XMLMapperBuilder

XMLMapperBuilder#parse

public void parse() {

if (!configuration.isResourceLoaded(resource)) { // @1

configurationElement(parser.evalNode(“/mapper”));

configuration.addLoadedResource(resource);

bindMapperForNamespace();

}

parsePendingResultMaps(); // @2

parsePendingChacheRefs(); // @3

parsePendingStatements(); // @4

}

代码@1:如果该映射文件(*.Mapper.xml)文件未加载,则首先先加载,完成xml文件的解析,提取xml中与mybatis相关的数据,例如sql、resultMap等等。

代码@2:处理mybatis xml中ResultMap。

代码@3:处理mybatis缓存相关的配置。

代码@4:处理mybatis statment相关配置,这里就是本篇关注的,Sql语句如何与Mapper进行关联的核心实现。

接下来我们重点探讨parsePendingStatements()方法,解析statement(对应SQL语句)。

2.2.1 XMLMapperBuilder#parsePendingStatements

private void parsePendingStatements() {

Collection incompleteStatements = configuration.getIncompleteStatements();

synchronized (incompleteStatements) {

Iterator iter = incompleteStatements.iterator(); // @1

while (iter.hasNext()) {

try {

iter.next().parseStatementNode(); // @2

iter.remove();

} catch (IncompleteElementException e) {

// Statement is still missing a resource…

}

}

}

}

代码@1:遍历解析出来的所有SQL语句,用的是XMLStatementBuilder对象封装的,故接下来重点看一下代码@2,如果解析statmentNode。

2.2.2 XMLStatementBuilder#parseStatementNode

public void parseStatementNode() {

String id = context.getStringAttribute(“id”); // @1 start

String databaseId = context.getStringAttribute(“databaseId”);

if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) return;

Integer fetchSize = context.getIntAttribute(“fetchSize”);

Integer timeout = context.getIntAttribute(“timeout”);

String parameterMap = context.getStringAttribute(“parameterMap”);

String parameterType = context.getStringAttribute(“parameterType”);

Class<?> parameterTypeClass = resolveClass(parameterType);

String resultMap = context.getStringAttribute(“resultMap”);

String resultType = context.getStringAttribute(“resultType”);

String lang = context.getStringAttribute(“lang”);

LanguageDriver langDriver = getLanguageDriver(lang);

Class<?> resultTypeClass = resolveClass(resultType);

String resultSetType = context.getStringAttribute(“resultSetType”);

StatementType statementType = StatementType.valueOf(context.getStringAttribute(“statementType”, StatementType.PREPARED.toString()));

ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);

String nodeName = context.getNode().getNodeName();

SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));

boolean isSelect = sqlCommandType == SqlCommandType.SELECT;

boolean flushCache = context.getBooleanAttribute(“flushCache”, !isSelect);

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());

// Parse selectKey after includes and remove them.

processSelectKeyNodes(id, parameterTypeClass, langDriver); // @1 end

// Parse the SQL (pre: and were parsed and removed)

SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass); // @2

String resultSets = context.getStringAttribute(“resultSets”);

String keyProperty = context.getStringAttribute(“keyProperty”);

String keyColumn = context.getStringAttribute(“keyColumn”);

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))

? new Jdbc3KeyGenerator() : new NoKeyGenerator();

}

builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, // @3

fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,

resultSetTypeEnum, flushCache, useCache, resultOrdered,

keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);

}

这个方法有点长,其关注点主要有3个:

代码@1:构建基本属性,其实就是构建MappedStatement的属性,因为MappedStatement对象就是用来描述Mapper-SQL映射的对象。

代码@2:根据xml配置的内容,解析出实际的SQL语句,使用SqlSource对象来表示。

代码@3:使用MapperBuilderAssistant对象,根据准备好的属性,构建MappedStatement对象,最终将其存储在Configuration中。

2.2.3 Configuration#addMappedStatement

public void addMappedStatement(MappedStatement ms) {

mappedStatements.put(ms.getId(), ms);

}

MappedStatement的id为:mapperInterface + methodName,例如com.demo.dao.UserMapper.findUser。

即上述流程完成了xml的解析与初始化,对终极目标是创建MappedStatement对象,上一篇文章介绍了mapperInterface的初始化,最终会初始化为MapperProxy对象,那这两个对象如何关联起来呢?

从下文可知,MapperProxy与MappedStatement是在调用具Mapper方法时,可以根据mapperInterface.getName + methodName构建出MappedStatement的id,然后就可以从Configuration的mappedStatements容器中根据id获取到对应的MappedStatement对象,这样就建立起联系了。

其对应的代码:

// MapperMethod 构造器

public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {

this.command = new SqlCommand(config, mapperInterface, method);

this.method = new MethodSignature(config, method);

}

// SqlCommand 构造器

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) throws BindingException {

String statementName = mapperInterface.getName() + “.” + method.getName();

MappedStatement ms = null;

if (configuration.hasStatement(statementName)) {

ms = configuration.getMappedStatement(statementName);

} else if (!mapperInterface.equals(method.getDeclaringClass().getName())) { // issue #35

String parentStatementName = method.getDeclaringClass().getName() + “.” + method.getName();

if (configuration.hasStatement(parentStatementName)) {

ms = configuration.getMappedStatement(parentStatementName);

}

}

if (ms == null) {

throw new BindingException("Invalid bound statement (not found): " + statementName);

}

name = ms.getId();
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

总结

无论是哪家公司,都很重视高并发高可用的技术,重视基础,重视JVM。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。

最后我整理了一些面试真题资料,技术知识点剖析教程,还有和广大同仁一起交流学习共同进步,还有一些职业经验的分享。

面试了阿里,滴滴,网易,蚂蚁,最终有幸去了网易【面试题分享】

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
munity.csdnimg.cn/images/e5c14a7895254671a72faed303032d36.jpg" alt=“img” style=“zoom: 33%;” />

总结

无论是哪家公司,都很重视高并发高可用的技术,重视基础,重视JVM。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。

最后我整理了一些面试真题资料,技术知识点剖析教程,还有和广大同仁一起交流学习共同进步,还有一些职业经验的分享。

[外链图片转存中…(img-l6T5SjpE-1713751076226)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 17
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值