mybatis sql in 查詢


 

本文轉自:http://www.blogjava.net/xmatthew/archive/2011/08/31/355879.html

1. 當查詢的參數只有一個時 
  findByIds(List<Long> ids)
 1.1 如果參數的類型是List, 則在使用時,collection屬性要必須指定為 list

Xml代碼  
  1. <select id="findByIdsMap" resultMap="BaseResultMap">  
  2.  Select  
  3.  <include refid="Base_Column_List" />  
  4.  from jria where ID in  
  5.  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
  6.   #{item}  
  7.  </foreach>  
  8. </select>  

 
 
 findByIds(Long[] ids)
 1.2 如果參數的類型是Array,則在使用時,collection屬性要必須指定為 array

Xml代碼  
  1. <select id="findByIdsMap" resultMap="BaseResultMap">  
  2. select  
  3. <include refid="Base_Column_List" />  
  4. from tabs where ID in  
  5. <foreach item="item" index="index" collection="array" open="(" separator="," close=")">  
  6.  #{item}  
  7. </foreach>  
  8.    </select>  

 
2. 當查詢的參數有多個時,例如 findByIds(String name, Long[] ids)
 這種情況需要特別注意,在傳參數時,一定要改用Map方式, 這樣在collection屬性可以指定名稱
         下面是一個示例
         Map<String, Object> params = new HashMap<String, Object>(2);
         params.put("name", name);
         params.put("ids", ids);
        mapper.findByIdsMap(params);

Xml代碼  
  1. <select id="findByIdsMap" resultMap="BaseResultMap">  
  2.  select  
  3.  <include refid="Base_Column_List" />  
  4.  from tabs where ID in  
  5.  <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">  
  6.   #{item}  
  7.  </foreach>  
  8. </select>  

 mybatis官方學習文檔:http://www.mybatis.org/core/getting-started.html 


免責聲明!

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



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