原文链接
http://opensource.atlassian.com/confluence/oss/display/IBATIS/How+do+I+reuse+SQL-fragments
When writing SqlMaps, you often encounter duplicate fragments of SQL, for example a FROM-clause or constraint-statement; iBATIS offers a simple yet powerful tag to reuse them. For the sake of simplicity, let's assume we want to get some items and we want to do a count on them.
Normally, you would write something like this:
当我们写SqlMaps的时候,经常会碰到重复的SQL片段,例如From语句或者约束条件;iBATIS提供了一个强大的标签来复用这些重复片段,简单举例,我们想检索一些字段,并且想统计它们。
通常情况下,你会这样写:
?To eliminate this duplication, we use the tags 【sql】 and 【include】. The 【sql】-tag contains the fragment to reuse, the 【include】-tag to include such a fragment:
为了消除重复片段,我们使用【sql】和【include】标签。【sql】标签用来包含重复片段,【include】标签用来引入片段:
?xml 代码
?The 【include】-tag is namespace-aware so you can refer to fragments even when they are located in another map (however, due to the way iBATIS loads the SqlMaps, the included fragment should be loaded before the including statement).?
【inclued】标签是一个命名空间可知的,所以你可以引入其他map的片段.(但是,因为iBATIS引入SqlMap的顺序,被引入的片段,要优先于欲引入的sql部分被导入)
The fragments are included and processed on query-execution so parameters can be used too:
重复片段在查询执行时被引入和执行,所以参数依然可以使用:
?xml 代码
我觉得楼主给的例子意义不大,但是在分页中用这个很不错,尤其是多条件查询,count和list分别写一个确实太麻烦
这可能要在 iBatis 底层进行修改,以提供更方便的使用接口。
目前还没认真想过如何结合,等遇到这样的需求时再看看。
ajoo 的 JRC:http://www.iteye.com/topic/21903
怎么没记录!?
我下载的ibatis-2.3.0.677的iBATIS-SqlMaps-2_en.pdf就记录了。
我是在这里找到的,我在2.0里面的中文没有找到:)
<sqlMap namespace="common"> <sql id="selectItem_fragment"> FROM items WHERE parentid = 6 </sql></sqlMap><sqlMap namespace="test"> <select id="selectItems" resultClass="Item"> SELECT id, name 错误 <include refid="common.selectItem_fragment"/> 正确 <include refid="selectItem_fragment"/> </select></sqlMap>
原先没看到这一块,谢谢了!
至于说例子 好像没有几个例子是有意义的 主要就是用最简单的方式让别人明白怎么用就行了
在这个include里面可不可以放变量,从程序外面传进去
<div class='code_title'/>
<div class='code_title'>xml 代码</div>
<div class='dp-highlighter'>
<div class='bar'/>
<ol class='dp-xml'>
<li class='alt'><span><span class='tag'><</span><span class='tag-name'>statement</span><span>?</span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"search"</span><span>?</span><span class='attribute'>resultMap</span><span>=</span><span class='attribute-value'>"infoResult"</span><span class='tag'>></span><span>??</span></span></li>
<li class=''><span>????????SELECT? ??</span></li>
<li class='alt'><span>????????????lnid, ??</span></li>
<li class=''><span>????????????tpye, ??</span></li>
<li class='alt'><span>????????????dsc, ??</span></li>
<li class=''><span>????????????pnid, ??</span></li>
<li class='alt'><span>????????FROM?F48100? ??</span></li>
<li class=''><span>????????</span><span class='tag'><</span><span class='tag-name'>include</span><span>?</span><span class='attribute'>refid</span><span>=</span><span class='attribute-value'>"Dy_SC"</span><span class='tag'>/></span><span>??</span></li>
<li class='alt'><span>????????ORDER?BY?bcdat?DESC ??</span></li>
<li class=''><span>????</span><span class='tag'></</span><span class='tag-name'>statement</span><span class='tag'>></span><span>??</span></li>
</ol>
</div>
<p>?使用的时候include就OK了,如下</p>
<div class='code_title'>
<div class='code_title'>xml 代码</div>
<div class='dp-highlighter'>
<div class='bar'/>
<ol class='dp-xml'>
<li class='alt'><span><span class='tag'>??? <</span><span class='tag-name'>sql</span><span>?</span><span class='attribute'>id</span><span>=</span><span class='attribute-value'>"Dy_SC"</span><span class='tag'>></span><span>??</span></span></li>
<li class=''><span>????????</span><span class='tag'><</span><span class='tag-name'>dynamic</span><span>?</span><span class='attribute'>prepend</span><span>=</span><span class='attribute-value'>"WHERE"</span><span class='tag'>></span><span>??</span></li>
<li class='alt'><span>????????????</span><span class='tag'><</span><span class='tag-name'>isNotNull</span><span>?</span><span class='attribute'>prepend</span><span>=</span><span class='attribute-value'>"AND"</span><span>?</span><span class='attribute'>property</span><span>=</span><span class='attribute-value'>"pnid"</span><span class='tag'>></span><span>??</span></li>
<li class=''><span>????????????????</span><span class='attribute'>pnid</span><span>?=?#pnid# ??</span></li>
<li class='alt'><span>????????????</span><span class='tag'></</span><span class='tag-name'>isNotNull</span><span class='tag'>></span><span>??</span></li>
<li class=''><span>????????????</span><span class='tag'><</span><span class='tag-name'>isNotNull</span><span>?</span><span class='attribute'>prepend</span><span>=</span><span class='attribute-value'>"AND"</span><span>?</span><span class='attribute'>property</span><span>=</span><span class='attribute-value'>"dsc"</span><span class='tag'>></span><span>??</span></li>
<li class='alt'><span>????????????????dsc?like?#dsc# ??</span></li>
<li class=''><span>????????????</span><span class='tag'></</span><span class='tag-name'>isNotNull</span><span class='tag'>></span><span>??</span></li>
<li class='alt'><span>????????</span><span class='tag'></</span><span class='tag-name'>dynamic</span><span class='tag'>></span><span>??</span></li>
<li class=''><span>????</span><span class='tag'></</span><span class='tag-name'>sql</span><span class='tag'>></span><span>??</span></li>
</ol>
</div>
</div>
<span class='tag'><span class='tag'><span class='tag'><span class='tag'><span class='tag'><span class='cdata'><span class='tag'><span class='tag'><span class='tag'>
<p>?</p>
<div class='code_title'>?</div>
<span class='tag'/></span></span></span></span></span></span></span></span></span>
如果一个xml很大或者id不在一个配置中,你要复用这个id,查找也是一种成本.
并且,好像嵌套做得不好!
比如:
select * from (XX) where rownum <= 10
这里的XX到底是放include里还是select里?
一般XX是会变的,肯定是写在select里对应一个ibatis dao方法,那么include就难做了.
它不能传参数,不能嵌套.
比较丑陋的,就是 select * from ( 定义为一个include
而 ) where rownum <= 10定义为另一个include
晕了,以前我怎么说namespace根本不起作用,不写也不会出错,原来还要这个配置。