mybatis sql in 查詢

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

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

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

findByIds(Long[] ids)mybatis

 1.2 若是參數的類型是Array,則在使用時,collection屬性要必須指定爲 arrayapp

Xml代碼  
 <select id="findByIdsMap" resultMap="BaseResultMap">
   select
   <include refid="Base_Column_List" />
   from tabs where ID in
   <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
    #{item}
   </foreach>
 </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代碼   
<select id="findByIdsMap" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from tabs where ID in
 <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
  #{item}
 </foreach>
</select>
相關文章
相關標籤/搜索