1. 程式人生 > >iBATIS動態標籤

iBATIS動態標籤

ibatis動態查詢:iBATIS動態查詢的實現淺析  

 iBATIS動態查詢實現主要是在iBATIS中使用安全拼接語句,動態查詢  iBATIS比JDBC的優勢:安全高效 

 iBATIS動態查詢例子:(介紹說明文字在註釋中)  

<select id ="selectAllProducts" parameterClass ="Product" resultMap ="ProductResult">  select id,note from Product  <dynamic prepend ="WHERE"> 

<!-- isNotNull判斷引數是否存在,Integer型別 --> <isNotNull property ="id"> 

<!-- isGreaterThan判斷引數是否大於compareValue,isGreaterEquals是大於等於 --> <isGreaterThan prepend =" and " property ="id" compareValue ="0">               id = #id#  </isGreaterThan> </isNotNull> 

<!-- isNotEmpty判斷字串不為空,isEmpty可以判斷字串為空 --> <isNotEmpty prepend =" and " property ="note"> 

<!-- 模糊查詢不能用#,#在是用prepareStatement?插入引數,$是文字替換 -->           note like '%$note$%'  </isNotEmpty> </dynamic> </select>   

 iBATIS動態查詢解釋:  

用Map傳引數  

<select id ="selectAllProducts" parameterClass ="java.util.HashMap" resultMap 

="ProductResult"> 

 select id,note from Product  <dynamic prepend ="WHERE"> 

<!-- isPropertyAvailable判斷屬性是否有效 --> <isPropertyAvailable property ="id"> <isNotNull property ="id"> 

<!-- isLessThan判斷引數是否小於compareValue,isLessEquals是小於等於 --> <isLessThan prepend =" and " property ="id" compareValue ="10">        id = #id#  </isLessThan> </isNotNull> 

</isPropertyAvailable> </dynamic>  </select>   

iBATIS動態查詢幾個常用屬性

< isPropertyAvailable > 屬性是存在  

< isNotPropertyAvailable > 屬性不存在  

< isNull > 屬性值是null  

< isEmpty > 判斷Collection.size < 1 或String.length<1  

<isEqual > 等於  

< isNotEqual > 不等於  

< isGreaterThan > 大於  

< isGreaterEqual > 大於等於  

< isLessThan > 小於  

< isLessEqual > 小於等於  

 iBATIS動態查詢相關資訊就向你介紹到這裡希望對你瞭解iBATIS動態查詢有所幫助

§<dynamic>標籤
§二元標籤
§一元標籤
§<iterate>標籤
§ 共同的屬性 prepend,open,close
<dynamic>標籤

§<dynamic>標籤
§屬性 prepend,open,close
二元條件標籤

§<isEqual property=“age”compareValue=“20”> 
比較屬性值和靜態值或另一個屬性值是否相等 

§<isNotEqual> 
比較屬性值和靜態值或另一個屬性值是否不相等。 

§<isGreaterThan> 
比較屬性值是否大於靜態值或另一個屬性值。 

§<isGreaterEqual> 
比較屬性值是否大於等於靜態值或另一個屬性值。 

§<isLessThan> 
比較屬性值是否小於靜態值或另一個屬性值。 

§<isLessEqual> 
比較屬性值是否小於等於靜態值或另一個屬性值。 

一元條件標籤

§<isPropertyAvailable> 
檢查是否存在該屬性(存在parameter bean的屬性) 

§<isNotPropertyAvailable> 
檢查是否不存在該屬性(不存在parameter bean的屬性) 

§<isNull> 
檢查屬性是否為null 

§<isNotNull> 
檢查屬性是否不為null 

§<isEmpty> 
檢查Collection.size()的值,屬性的String或String.valueOf()值,是否為null或空(“”或size() < 1) 

§<isNotEmpty> 
檢查Collection.size()的值,屬性的String或String.valueOf()值,是否不為null或不為空(“”或size() > 0)

其他標籤

§<isParameterPresent> 
檢查是否存在引數物件(不為null)

§<isNotParameterPresent> 
檢查是否不存在引數物件(引數物件為null)

§<iterate> 
遍歷型別為java.util.List的元素。 

view plaincopy toclipboardprint?
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map2.0//EN" 
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>

    <!-- 動態查詢1-->
    <select id="getOrders1"parameterClass="com.air.Account"
       resultClass="com.air.Product">
        SELECT  
            orders.id as id,  
            orders.product asproduct, 
            orders.customer ascustomer  
        FROM orders 
        <dynamic prepend=" WHERE">
            <isNullproperty="username">customer IS NOT NULL</isNull>
            <isNotNullproperty="username">
               orders.customer=#username# 
            </isNotNull>
        </dynamic>
    </select>

    <!-- 動態查詢2-->
    <select id="getOrders2"parameterClass="com.air.Account"
       resultClass="com.air.Product">
        SELECT  
            o.id as id,  
            o.product as product, 
            o.customer as customer 
        FROM orders o  
            <isNotNullproperty="username">
                INNER JOIN user_account a ONo.customer=a.username  
            </isNotNull>
            <dynamicprepend="where ">
                <isNotEmptyproperty="groupname" prepend="and " open="("close=")">
                   a.groupname=#groupname# 
                </isNotEmpty>
            </dynamic>
    </select>

    <!-- 動態查詢3-->
    <select id="getOrders3"parameterClass="com.air.Product"
       resultClass="com.air.Product">
        SELECT  
            orders.id as id,  
            orders.product asproduct, 
            orders.customer ascustomer  
        FROM orders  
        <dynamic prepend="WHERE">
            <isNotEmptyproperty="product" prepend="and" open="("close=")" removeFirstPrepend="true">
                product=#product# 
            </isNotEmpty>
            <isNotEmptyproperty="customer" prepend="and" open="("close=")">
                customer=#customer# 
            </isNotEmpty>
        </dynamic>
    </select>
     
    <!-- 動態查詢4-->
    <select id="getOrders4"parameterClass="com.air.Product"
       resultClass="com.air.Product">
        SELECT  
            * 
        FROM orders  
        <dynamic prepend="WHERE">
            <iterateproperty="keywords" open="(" close=")"conjunction="OR">  
                product=#keywords[]# 
            </iterate>
        </dynamic>
<!--     <dynamicprepend="WHERE product IN ">-->
<!--         <iterateproperty="keywords" open="(" close=")"conjunction=","> -->
<!--             #keywords[]#-->
<!--         </iterate>-->
<!--     </dynamic>-->
    </select>
</sqlMap>
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapPUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap>

<!-- 動態查詢1-->
<select id="getOrders1" parameterClass="com.air.Account"
  resultClass="com.air.Product">
   SELECT
    orders.id as id,
    orders.product as product,
    orders.customer as customer
   FROM orders
   <dynamic prepend=" WHERE">
    <isNullproperty="username">customer IS NOT NULL</isNull>
    <isNotNullproperty="username">
     orders.customer=#username#
    </isNotNull>
   </dynamic>
</select>

<!-- 動態查詢2-->
<select id="getOrders2" parameterClass="com.air.Account"
   resultClass="com.air.Product">
   SELECT
    o.id as id,
    o.product as product,
    o.customer as customer
   FROM orders o
    <isNotNullproperty="username">
     INNER JOIN user_account a ONo.customer=a.username
    </isNotNull>
    <dynamic prepend="where">
     <isNotEmptyproperty="groupname" prepend="and " open="("close=")">
      a.groupname=#groupname#
     </isNotEmpty>
    </dynamic>
</select>

<!-- 動態查詢3-->
<select id="getOrders3" parameterClass="com.air.Product"
   resultClass="com.air.Product">
   SELECT
    orders.id as id,
    orders.product as product,
    orders.customer as customer
   FROM orders
   <dynamic prepend="WHERE">
    <isNotEmptyproperty="product" prepend="and" open="("close=")" removeFirstPrepend="true">
     product=#product#
    </isNotEmpty>
    <isNotEmptyproperty="customer" prepend="and" open="("close=")">
     customer=#customer#
    </isNotEmpty>
   </dynamic>
</select>

<!-- 動態查詢4-->
<select id="getOrders4" parameterClass="com.air.Product"
   resultClass="com.air.Product">
   SELECT
    *
   FROM orders
   <dynamic prepend="WHERE">
    <iterateproperty="keywords" open="(" close=")"conjunction="OR">
     product=#keywords[]#
    </iterate>
   </dynamic>
<!--   <dynamic prepend="WHEREproduct IN ">-->
<!--    <iterateproperty="keywords" open="(" close=")"conjunction=","> -->
<!--     #keywords[]#-->
<!--    </iterate>-->
<!--   </dynamic>-->
</select>
</sqlMap>