iBatis高级应用(中)--动态SQL

九:动态SQL

动态SQL里面的标签类似STRUTS里面的标签,不多说,看看配置就行了!

<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 ">
           <isNull property="username">customer IS NOT NULL</isNull>
           <isNotNull property="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
           <isNotNull property="username">
              INNER JOIN user_account a ON o.customer=a.username
           </isNotNull>
           <dynamic prepend="where ">
<isNotEmpty property="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 ">
    <isNotEmpty property="product" prepend="and" open="(" close=")" >
              product=#product#
           </isNotEmpty>
    <isNotEmpty property="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  ">
    <iterate property="keywords"  open="(" close=")" conjunction="OR">
              product=#keywords[]#
           </iterate>
       </dynamic>
<!--       <dynamic prepend="WHERE product IN  ">-->
<!--          <iterate property="keywords"  open="(" close=")" conjunction=","> -->
<!--              #keywords[]#-->
<!--          </iterate>-->
<!--       </dynamic>-->
    </select>
</sqlMap>

 

你可能感兴趣的:(ibatis)