首页 > 其他 > 详细

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

时间:2016-09-19 19:18:57      阅读:269      评论:0      收藏:0      [点我收藏+]

 这是我们的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>

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

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

原文:http://www.cnblogs.com/zuge/p/5886039.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!