MyBatis 常用標簽用法


 

標簽

<foreach collection="" item="" index=""  open="" separator="" close=""></foreach>
collection為集合名
item 表示集合中每一個元素 進行迭代時的別名,
index 指定一個名字,用於表示在迭代過程中,每次迭代到的位置,
open 表示該語句以什么開始,
separator 表示在每次進行迭代之間以什么符號作為分隔 符,
close 表示以什么結束。

示例:
dao層方法代碼
List<Customer>  selectByIds(@Param("ids") Integer[] ids);

 mapper使用foreach標簽

    <select id="selectByIds" resultType="Customer">
        select *
          from WXBHPT_V_Customers
          <where>
              <if test='ids!=null '>
                  customid  in
                  <foreach collection="ids" item="id" open="(" separator="," close=")" index="index">
                    #{id}
                  </foreach>
              </if>
          </where>
       order by customid;
    </select>

  拼接結果為:

select * from WXBHPT_V_Customers where customid in (?,?,?)

  ps:如果ids是某個bean對象的集合或數組,  #{id.xxx}   (xxx為屬性名) 可直接取值

 

bind標簽

 <bind  name="" value="" />
 name為變量取一個名字,以便在后面使用它
value的值

 用法舉例

      dao代碼

Integer count(@Param("search") String search);

  mapper代碼

<select id="count" resultType="Integer"> 
  <bind name="pattern" value="'%' + _parameter.search + '%'" />
  select count(*) from WXBHPT_V_Customers where search like #{pattern} order by customid
</select>

  拼接結果

select count(*) from WXBHPT_V_Customers where search like %searchstr% order by customid 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM