在工作中,往往有这样的需求,对于同一个sql条件查询,首先需要统计记录条数,用以计算pageCount,然后再对结果进行分页查询显示,看下面一个例子。

<sql id="studentProperties"><!--sql片段-->select stud_id as studId, name, email, dob, phonefrom 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,其可读性就非常差了。

sqlFragment里的内容是可以随意写的,它不需要是一个完整的sql,它可以是“,phone”这么简单的文本。

1.sqlFragment的解析过程

sqlFragment存储于Configuration内部。

protected final Map<String, XNode> sqlFragments = new StrictMap<XNode>("XML fragments parsed from previous mappers");

解析sqlFragment的过程非常简单。

org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XNode)方法部分源码。

// 解析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提供服务的。

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 nodeProperties fullContext;String refid = getStringAttribute(source, "refid");// replace variables in include refid valuerefid = PropertyParser.parse(refid, variablesContext);Node toInclude = findSqlFragment(refid);Properties newVariablesContext = getVariablesContext(source, variablesContext);if (!newVariablesContext.isEmpty()) {// merge contextsfullContext = new Properties();fullContext.putAll(variablesContext);fullContext.putAll(newVariablesContext);} else {// no new context - use inherited fullyfullContext = 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 valuessource.setNodeValue(PropertyParser.parse(source.getNodeValue(), variablesContext));} else if (source.getNodeType() == Node.TEXT_NODE && !variablesContext.isEmpty()) {// replace variables ins all text nodessource.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, phonefrom 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, phonefrom students<sql id="studentProperties">select stud_id as studId, name, email, dob, phonefrom students</sql>
) tmp</select>

④移除sqlFragment节点

<select id="countAll" resultType="int">select count(1) from (select stud_id as studId, name, email, dob, phonefrom students
) tmp</select>

⑤最终结果如图所示

如此一来,TextNode1 + TextNode2 + TextNode3,就组成了一个完整的sql。遍历select的三个子节点,分别取出TextNode的value,append到一起,就是最终完整的sql。

这也是为什么要移除<selectKey> and <include>节点的原因。

这就是Mybatis的sqlFragment,以上示例,均为静态sql,即static sql。

最后

感谢大家看到最后,如文章有不足,欢迎大家在评论区支持,给予意见。如果觉得我的文章对你有帮助,那就给我一个赞同吧。

每天都会分享java相关技术文章或行业资讯。欢迎大家关注和转发文章。

sql if 和insert_拼多多面试:Mybatis是如何实现SQL语句复用功能的?相关推荐

  1. 拼多多面试官没想到RabbitMQ我早就入门了,人直接傻掉

    拼多多面试官没想到RabbitMQ我早就入门了,人直接傻掉. 人一辈子最值得炫耀的不应该是你的财富有多少(虽然这话说得有点违心,呵呵),而是你的学习能力.技术更新迭代的速度非常快,那作为程序员,我们就 ...

  2. 5图片展示_拼多多搜索【智能创意】推广5大功能升级!

    现在,拼多多搜索推广已正式更名为"多多搜索",在这之后也有不少的优化更多,智能推广就是其中一个.拼多多搜索智能创意是可以根据不同用户偏好来展示适合的创意,能有效提升10%的点击率. ...

  3. 拼多多在海外暂时不会上线砍一刀功能;微软落户中国三十周年;JDK 19 GA发布|极客头条

    「极客头条」-- 技术人员的新闻圈! CSDN 的读者朋友们早上好哇,「极客头条」来啦,快来看今天都有哪些值得我们技术人关注的重要新闻吧. 整理 | 梦依丹 出品 | CSDN(ID:CSDNnews ...

  4. 拼多多面试:如何用 Redis 统计独立用户访问量?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 作者:沙茶敏碎碎念 来源:https://url.cn/5tQPE ...

  5. 拼多多面试|如何用 Redis 统计独立用户访问量?

    众所周至,拼多多的待遇也是高的可怕,在挖人方面也是不遗余力,对于一些工作3年的开发,稍微优秀一点的,都给到30K的Offer 当然,拼多多加班也是出名的,一周上6天班是常态,每天工作时间基本都是超过1 ...

  6. 拼多多面试真题:如何用 Redis 统计独立用户访问量!

    作者 | 沙茶敏碎碎念 来源 | http://toutiao.com/i6695734985246114312/ 众所周至,拼多多的待遇也是高的可怕,在挖人方面也是不遗余力,对于一些工作3年的开发, ...

  7. 2019校招内推拼多多面试总结

    感想 我想这次应该是我凭实力得到的第一个比较满意的offer了,没想到是拼多多给的offer,我也感谢面试我的那些面试官,没有为难我. 过程 拼多多 一面 1.自我介绍 2.树模型,GBDT的原理,x ...

  8. Mybatis是如何实现SQL语句复用功能的?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | https://my.oschina.net/ ...

  9. 阿里,腾讯,拼多多面试必挂:面对千万级、亿级流量怎么处理?

    这是一道很常见的面试题,但是大多数人并不知道怎么回答,这种问题其实可以有很多形式的提问方式,你一定见过而且感觉无从下手: 面对业务急剧增长你怎么处理? 业务量增长10倍.100倍怎么处理? 你们系统怎 ...

最新文章

  1. 灭霸—个人冲刺(4)
  2. IBM XIV高效存储广受公安、医疗、电信、金融等客户赞誉
  3. 查询数据库游标使用情况以及sql
  4. 米其林餐厅 盐之花_在世界范围内探索《米其林指南》
  5. CompletableFuture详解~thenAccept
  6. 在JSP中使用JavaBean
  7. 19【推荐系统4】DeeoCrossing
  8. 最快理解使用CSS弹性盒子
  9. JZOJ 4.2 C组 打鼹鼠
  10. CentOS防火墙放行端口(以 8080 端口为例)
  11. Java网页版仿QQ实现在线聊天功能
  12. Assembler如何把跳转汇编变成机器码的(一)
  13. 爬虫学习01 什么是爬虫 爬虫的分类
  14. npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock
  15. flv怎么转换成mp4格式?
  16. 区块链与联邦学习综述
  17. python爬虫之scrapy初试与抓取链家成交房产记录
  18. 51单片机c语言led灯闪烁实验报告,实验一LED灯闪烁.doc
  19. Java implements和extends的区别
  20. 51NOD 1024 矩阵中不重复的元素

热门文章

  1. linux之用route命令看简单路由信息
  2. 怎样理解Linux的文件系统
  3. python3.6字典有序_为什么从Python 3.6开始字典有序并效率更高
  4. 手机浏览器html5游戏,移动浏览器都爱 HTML5 ?
  5. 波的折射现象,你都了解吗?
  6. 你永远都不知道你老公可以多幼稚......
  7. 你根本想象不到,学霸到底经历过什么
  8. 程序猿都在关注的6个优质公众号
  9. 3 年工作经验程序员应有的技能
  10. 为什么电脑不能打字_嘉兴在线丨「生活经济学」为什么笔记本电脑能在任何国家的供电标准下运作,其他大部分电器却不能?...