Ibatis Memo

Reusing SQL Fragments
we use the tags <sql></sql>and <include></include>. The <sql></sql>tag contains the fragment to reuse, the <include></include>tag includes such a fragment in a statement. For example:

  1. <sql id="selectItem_fragment">  
  2. FROM items   
  3. WHERE parentid = 6  
  4. sql>  
  5.   
  6. <select id="selectItemCount" resultClass="int">  
  7.   SELECT COUNT(*) AS total   
  8.   <include refid="selectItem_fragment"/>  
  9. select>  
  10.   
  11. <select id="selectItems" resultClass="Item">  
  12. SELECT id, name   
  13.   <include refid="selectItem_fragment"/>  
  14. select>  


The fragments are included and processed on query-execution so parameters can be used too:

  1. <sql id="selectItem_fragment">  
  2.   FROM items   
  3.   WHERE parentid = #value#   
  4. sql>  
  5.   
  6. <select id="selectItemCount" parameterClass="int" resultClass="int">  
  7.   SELECT COUNT(*) AS total   
  8.   <include refid="selectItem_fragment"/>  
  9. select>  
  10.   
  11. <select id="selectItems" parameterClass="int" resultClass="Item">  
  12.   SELECT id, name   
  13.   <include refid="selectItem_fragment"/>  
  14. select>  



Auto-Generated Key

  1. >  
  2. <insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">  
  3.     <selectKey resultClass="int" >  
  4.         SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL   
  5.     selectKey>  
  6.     insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)   
  7.     values (#id#,#description#)   
  8. insert>  



The selectKey statement is executed before the insert statement if it is placed before the insert SQL, otherwise the selectKey statement is executed after the insert statement.

It improve in 2.0

  1. <insert id="insertProduct-ORACLE-type-specified" parameterClass="com.domain.Product">  
  2.     insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)   
  3.     values (#id#,#description#)   
  4.     <selectKey resultClass="int" type="pre" >  
  5.         SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL   
  6.     selectKey>  
  7. insert>  

Substitution Strings

select * from $tableName$

With this syntax, iBATIS will substitute the value of the “tableName” property into the SQL before the statement is prepared. With this support, you can substitute string into any part of an SQL statement.
Important Note 1: This support will only substitute Strings, so it is not appropriate for complex data types like Date or Timestamp.
Important Note 2: If you use this support to alter a table name, or a column list, in an SQL select statement, then you should always specify remapResults=“true”.

iBATIS provides syntax for substituting strings into SQL before the statement is prepared. You can use this support to generate a dynamic SQL statement. An example of the substitution syntax is as follows:
   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值