Mybaits 之根据集合查询和逗号分隔的子查询

 这是我们的mapper要根据传入一个集合进行查询:

List<ExtKeywordCategory> findListByIds(List<ExtKeywordFkCategory> extKeywordFkCategoryList);

对应的xml

 <select id="findListByIds" resultType="com.ebrun.newcms.modules.extend.model.ExtKeywordCategory" parameterType="java.util.List">
    select 
    <include refid="Base_Column_List" />
    from ext_keywordCategory a
    <where>
    id in 
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
      #{item.categoryid}  
     </foreach>
    </where>
  </select>

关于foreach的属性

 


第二种

前台传入的格式为字符串的,例如89,100那么我们可以这么处理

mapper:

List<ExtKeywordCategory> getListByIds(@Param("categoryids") String categoryids);

xml:

<select id="getListByIds" resultType="com.ebrun.newcms.modules.extend.model.ExtKeywordCategory">
   select 
    <include refid="Base_Column_List" />
    from ext_keywordCategory 
    <where>
    id in (${categoryids})
    </where>
  </select>

 需要注意的是#和$的区别

posted @ 2016-09-19 17:44  atzuge  阅读(1462)  评论(0编辑  收藏  举报