Mybatis目前最新版本为3.4.0,所以,我也将个人项目由3.3.1替换为3.4.0。在上一篇博文中,详细分析了Mybatis在使用foreach循环进行批量insert,返回主键id列表时,若是使用BatchExecutor,那么因为Mybatis存在bug,返回的id列表将是null值。很遗憾的告诉你们,Mybatis3.4.0依然是没有修复该bug的,该bug依然存在。java
今天,咱们将分析Mybatis之sqlFragment,能够翻译为sql片断,它的存在价值在于可复用sql片断,避免处处重复编写。node
在工做中,每每有这样的需求,对于同一个sql条件查询,首先须要统计记录条数,用以计算pageCount,而后再对结果进行分页查询显示,看下面一个例子。sql
<sql id="studentProperties"><!--sql片断--> select stud_id as studId , name, email , dob , phone from students </sql> <select id="countAll" resultType="int"> select count(1) from ( <include refid="studentProperties"></include><!--复用--> ) tmp </select> <select id="findAll" resultType="Student" parameterType="map"> select * from ( <include refid="studentProperties"></include><!--复用--> ) tmp limit #{offset}, #{pagesize} </select>
这就是sqlFragment,它能够为select|insert|update|delete标签服务,能够定义不少sqlFragment,而后使用include标签引入多个sqlFragment。在工做中,也是比较经常使用的一个功能,它的优势很明显,复用sql片断,它的缺点也很明显,不能完整的展示sql逻辑,若是一个标签,include了四至五个sqlFragment,其可读性就很是差了。apache
sqlFragment里的内容是能够随意写的,它不须要是一个完整的sql,它能够是“,phone”这么简单的文本。网络
1.sqlFragment的解析过程app
sqlFragment存储于Configuration内部。dom
protected final Map<String, XNode> sqlFragments = new StrictMap<XNode>("XML fragments parsed from previous mappers");
解析sqlFragment的过程很是简单。ui
org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XNode)方法部分源码。spa
// 解析sqlFragment sqlElement(context.evalNodes("/mapper/sql")); // 为select|insert|update|delete提供服务 buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
sqlFragment存储于Map<String, XNode>结构当中。其实最关键的,是它如何为select|insert|update|delete提供服务的。.net
2.select|insert|update|delete标签中,解析include标签的过程
org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode()方法源码。
// 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); // Parse the SQL (pre: <selectKey> and <include> were parsed and removed) SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
注释“pre: <selectKey> and <include> were parsed and removed”,含义为解析完,并移除。为何要移除呢?秘密都隐藏在applyIncludes()方法内部了。
org.apache.ibatis.builder.xml.XMLIncludeTransformer.applyIncludes(Node, Properties)方法源码。
/** * Recursively apply includes through all SQL fragments. * @param source Include node in DOM tree * @param variablesContext Current context for static variables with values */ private void applyIncludes(Node source, final Properties variablesContext) { if (source.getNodeName().equals("include")) { // new full context for included SQL - contains inherited context and new variables from current include node Properties fullContext; String refid = getStringAttribute(source, "refid"); // replace variables in include refid value refid = PropertyParser.parse(refid, variablesContext); Node toInclude = findSqlFragment(refid); Properties newVariablesContext = getVariablesContext(source, variablesContext); if (!newVariablesContext.isEmpty()) { // merge contexts fullContext = new Properties(); fullContext.putAll(variablesContext); fullContext.putAll(newVariablesContext); } else { // no new context - use inherited fully fullContext = variablesContext; } // 递归调用 applyIncludes(toInclude, fullContext); if (toInclude.getOwnerDocument() != source.getOwnerDocument()) { toInclude = source.getOwnerDocument().importNode(toInclude, true); } // 将include节点,替换为sqlFragment节点 source.getParentNode().replaceChild(toInclude, source); while (toInclude.hasChildNodes()) { // 将sqlFragment的子节点(也就是文本节点),插入到sqlFragment的前面 toInclude.getParentNode().insertBefore(toInclude.getFirstChild(), toInclude); } // 移除sqlFragment节点 toInclude.getParentNode().removeChild(toInclude); } else if (source.getNodeType() == Node.ELEMENT_NODE) { NodeList children = source.getChildNodes(); for (int i=0; i<children.getLength(); i++) { // 递归调用 applyIncludes(children.item(i), variablesContext); } } else if (source.getNodeType() == Node.ATTRIBUTE_NODE && !variablesContext.isEmpty()) { // replace variables in all attribute values source.setNodeValue(PropertyParser.parse(source.getNodeValue(), variablesContext)); } else if (source.getNodeType() == Node.TEXT_NODE && !variablesContext.isEmpty()) { // replace variables ins all text nodes source.setNodeValue(PropertyParser.parse(source.getNodeValue(), variablesContext)); } }
上面是对源码的解读,为了便于理解,咱们接下来采用图示的办法,演示其过程。
3.图示过程演示
①解析节点
<select id="countAll" resultType="int"> select count(1) from ( <include refid="studentProperties"></include> ) tmp </select>
②include节点替换为sqlFragment节点
<select id="countAll" resultType="int"> select count(1) from ( <sql id="studentProperties"> select stud_id as studId , name, email , dob , phone from students </sql> ) tmp </select>
③将sqlFragment的子节点(文本节点)insert到sqlFragment节点的前面。注意,对于dom来讲,文本也是一个节点,叫TextNode。
<select id="countAll" resultType="int"> select count(1) from ( select stud_id as studId , name, email , dob , phone from students <sql id="studentProperties"> select stud_id as studId , name, email , dob , phone from students </sql> ) tmp </select>
④移除sqlFragment节点
<select id="countAll" resultType="int"> select count(1) from ( select stud_id as studId , name, email , dob , phone from students ) tmp </select>
⑤最终结果如图所示
(Made In QQ截图及时编辑)
如此一来,TextNode1 + TextNode2 + TextNode3,就组成了一个完整的sql。遍历select的三个子节点,分别取出TextNode的value,append到一块儿,就是最终完整的sql。
这也是为何要移除<selectKey> and <include>节点的缘由。
这就是Mybatis的sqlFragment,以上示例,均为静态sql,即static sql,有关动态sql,即dynamic sql,将在后续博文中进行仔细分析。
版权提示:文章出自开源中国社区,若对文章感兴趣,可关注个人开源中国社区博客(http://my.oschina.net/zudajun)。(通过网络爬虫或转载的文章,常常丢失流程图、时序图,格式错乱等,仍是看原版的比较好)