【Ibatis】(六)、动态SQL查询

映射文件:

Xml代码   收藏代码
  1. <select id="getProductDynamic2" resultMap="get-product-result" parameterClass="product">  
  2.             <![CDATA[ 
  3.                 select * from t_product 
  4.             ]]>    
  5.                 <dynamic prepend="WHERE">  
  6.                     <isNotNull prepend="AND" property="price">  
  7.                         prd_price=#price#  
  8.                     </isNotNull>  
  9.                     <isNotEmpty prepend="AND" property="description">  
  10.                         prd_description=#description#  
  11.                     </isNotEmpty>  
  12.                 </dynamic>  
  13.                   
  14.         </select>  

 注意:CDATA不应包括<dynamic>节点,否则标签不起作用!

DAO层:

Java代码   收藏代码
  1. public List getProductDynamic2(Product product) throws SQLException {  
  2.         init();  
  3.         List list = (List)sqlMapClient.queryForList("getProductDynamic2", product);  
  4.         return list;  
  5.     }  

 TEST类:

Java代码   收藏代码
  1. public void getProductDynamic2() throws SQLException {  
  2.         Product product = new Product();  
  3.         product.setPrice(206.99d);  
  4.         product.setDescription("basketball");  
  5.         List list1 = productDao.getProductDynamic2(product);  
  6.         for(Iterator it=list1.iterator(); it.hasNext();) {  
  7.             Product prd = (Product)it.next();  
  8.             System.out.println(prd);  
  9.         }  
  10.           
  11.           
  12.         /** 
  13.          * 注意:product2里的price(是double型,不是Double型)的值没有设置,所以默认为初始化时的值(0),而不是NULL, 
  14.            传入到sqlmap映射文件时,被包装成Double类型(ibatis中传入参数的都是引用类型),值为0, 
  15.            这对statement里的动态语句有影响! 
  16.          */  
  17.         Product product2 = new Product();  
  18.         product2.setDescription("basketball");  
  19.         List list2 = productDao.getProductDynamic2(product2);  
  20.         for(Iterator it=list1.iterator(); it.hasNext();) {  
  21.             Product prd = (Product)it.next();  
  22.             System.out.println(prd);  
  23.         }  
  24.     }  

 

一元判定 是针对属性值本身的判定,如属性是否为NULL,是否为空值等。

<isEmpty>
检查Collection.size()的值,属性的String或String.valueOf()值,是否为null或空(“”或size() < 1)。

二元判定 有两个判定参数,一是属性名,而是判定值,如

Xml代码   收藏代码
  1. <isGreaterThan prepend="AND" property="age" compareValue="18">  
  2.         (age=#age#)  
  3. </isGreaterThan>  
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iBatis动态SQL标签包括以下几种: 1. `<if>`标签:用于判断条件是否成立,如果成立则执行其中的SQL语句。示例代码: ``` <select id="selectPerson" parameterType="int" resultType="Person"> SELECT * FROM person WHERE 1=1 <if test="id != null"> AND id = #{id} </if> <if test="name != null"> AND name = #{name} </if> </select> ``` 2. `<choose>`标签:类似于Java中的switch语句,用于根据不同条件选择不同的SQL语句执行。示例代码: ``` <select id="selectPerson" parameterType="int" resultType="Person"> SELECT * FROM person <choose> <when test="id != null"> WHERE id = #{id} </when> <when test="name != null"> WHERE name = #{name} </when> <otherwise> WHERE 1=1 </otherwise> </choose> </select> ``` 3. `<where>`标签:用于动态拼接WHERE语句,自动去除多余的AND和OR关键字。示例代码: ``` <select id="selectPerson" parameterType="int" resultType="Person"> SELECT * FROM person <where> <if test="id != null"> AND id = #{id} </if> <if test="name != null"> AND name = #{name} </if> </where> </select> ``` 4. `<foreach>`标签:用于循环遍历集合或数组,动态生成SQL语句。示例代码: ``` <insert id="insertPersons" parameterType="List"> INSERT INTO person (id, name) VALUES <foreach collection="list" item="person" separator=","> (#{person.id}, #{person.name}) </foreach> </insert> ``` 5. `<set>`标签:用于动态拼接SET语句,用于更新操作。示例代码: ``` <update id="updatePerson" parameterType="Person"> UPDATE person <set> <if test="name != null"> name = #{name}, </if> <if test="age != null"> age = #{age}, </if> </set> WHERE id = #{id} </update> ``` 以上是iBatis中常用的动态SQL标签,可以根据实际需求选择适当的标签来组合使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值