Mybatis(動態sql)

這裏寫了一個帶條件的動態模糊查詢,大家首先聯想的是不是當這個查詢爲空得判斷是否爲空?用if標籤來判斷

select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id

      where 1=1

<if test="qname!=' '  ">    注意:這裏不能寫爲null

and     a.productname  like  '%${qname}%'

</if>

<if   test="qid != '請選擇'">   注意:這裏的請選擇指的JSP動態頁面中下拉框的選項 value="請選擇"

and     b.proname=#{qid}

</if>

<if test="ispay !='請選擇' ">

and  a.ispayment=#{ispay}

</if>

if標籤表示判斷,如果符合條件,則執行條件內容

這裏的qname,qid,ispay是通過接口中的方法用Hashmap集合存的  

HashMap<String, Object> map=new HashMap<String,Object>();

我們只知道鍵是String類型,並不知道值是什麼類型。所以定義一個Object

然後servlet中存的map.put("qname", "%"+queryProductName+"%");
                    //map.put("qname", queryProductName);
                      map.put("qid", queryProviderId);
                      map.put("ispay",queryIsPayment );

然後用where+if標籤代替

<select  id="getAllSmbmsBillByLike2" parameterType="java.util.HashMap" resultMap="rmap">
    select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id

 <where>     //where代替了where 1=1; 這個表結構
            <if test="qname!='' ">
              and a.productname like #{qname}       注意這裏的兩個%%在servlet已經完成拼接
              </if>
              <if test="qid !='請選擇' ">
              and  b.proname=#{qid}
              </if>
              <if test="ispay !='請選擇' ">
              and  a.ispayment=#{ispay}
              </if>
            </where>

</select>

where標籤,表示條件的連接符.如果判斷條件都不成立的時候,自動把where關鍵字去掉

如果成立的時候,會自動的把where 關鍵字後面的and連接符去掉

然後在利用trim標籤代替

<select  id="getAllSmbmsBillByLike2" parameterType="java.util.HashMap" resultMap="rmap">
    select a.id,
           a.billcode,
           a.productname,
           a.totalprice,
           a.ispayment,
           a.creationdate,
           b.proname
             from smbms_bill a
            inner join smbms_provider b on a.providerid = b.id
            <trim prefix="where" prefixOverrides="and">   //trim這個標籤使用場景比較廣泛,設置前綴是where
             <if test="qname!=' '  ">
               a.productname like '%${qname}%'
              </if>
              <if test="qid !='請選擇' ">
               and  b.proname=#{qid}
              </if>
              <if test="ispay !='請選擇' ">
               and  a.ispayment=#{ispay}
              </if>
            </trim>

</select>

trim表示去掉多餘的指定的字符,prefix表示前綴,suffix表示後綴,suffixOverrides去除字段之後的指定字符

      prefixOverrides去除字段之前的指定字符

set標籤 ,選擇性修改。

<update  id="updateSmbmsBill" parameterType="sbill">
        update smbms_bill
        <trim prefix="set" suffixOverrides=","    suffix="where id=#{id}">
            <if  test="billcode!=' '  ">
            billcode=#{billcode},
            </if>
            <if test="productname!='' ">
                   productname=#{productname},
            </if>
            <if test="productunit!='' "> 
                productunit=#{productunit},
            </if>
            <if test="productcount!='' ">
            productcount=#{productcount},
            </if>
            <if test="totalprice!='' ">
            totalprice=#{totalprice},
            </if>
            <if test="ispayment!='' ">
            ispayment=#{ispayment}
            </if>
        </trim>
    </update>

Foreach標籤,適用於批量查詢和批量刪除  (查詢與刪除同理)

注意:使用foreach,接口的參數類型有兩種,第一種是list集合,第二種是數組array類型。

這裏使用的是第二種數組List<SmbmsRole>  getAllUserInfoByForeach(String[] str);//批量查詢 

<select id="getAllUserInfoByForeach" resultMap="rmap2">
    select b.id, b.usercode, b.username, b.gender, 
          floor(months_between(sysdate,b.birthday)/12) as age,
            b.phone, a.rolename
               from smbms_role a
              inner join smbms_user b on a.id = b.userrole
              where b.userrole in       
              <!-- item:選項 這裏的屬性隨便取,要跟#{item}屬性值對應

                    注意查詢的時候使用的是in關鍵字
                   open:開始
                   separator:分隔符
                   close:結束
                   collection:集合是數組
               -->
              <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
              #{item}
              </foreach>
    </select>

然後在使用另一種list集合

//批量查詢

   public List selectProdcutByForeach(List pList);

<!-- foreach批量查詢

      foreach標籤表示循環標籤,collection表示集合屬性,屬性值有兩種,

      如果接口的參數是List類型,那麼該屬性值=list

      如果接口的參數是數組類型,那麼該屬性值=array

      open屬性表示類似於前綴

      close表示類似於後綴

      item表示集合的遍歷體,屬性值隨意起

      separator表示隔離間隔的關鍵字屬性,

      sql:SELECT * FROM product WHERE id IN(47,46,1,10)

      foreach標籤之間展示的每次遍歷的id值,表達形式#{item屬性值}

   -->

   <select id="selectProdcutByForeach" parameterType="java.util.List" resultType="product">

      select

        <include refid="basesql"/>

      from product

      <where>

        id in

        <foreach collection="list" open="(" item="aid" separator="," close=")">

           #{aid}

        </foreach>

      </where>

   </select>

 

Choose標籤(不建議使用)

Choose+when+otherwise聯合使用

當有一個when條件成立的時候則執行,那麼之後的when條件不管成立與否均不在執行

      當所有的when條件都不成立的時候,則執行otherwise條件

<select id="selectProductBySearchdong" parameterType="product" resultType="product">

      select

        <include refid="basesql"/>

      from product

   <where>

      <choose>

        <when test="price>0">

           and price=#{price}

        </when>

        <when test="name!=null">

           or name like '%${name}%'

        </when>

        <otherwise>

           and description=#{description}

        </otherwise>

      </choose>

   </where>

   </select>

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章