MyBatis中的in条件查询的用法代码示例

  1. 1.Dao对应的MyBatis的xml的配置如下:

xml开头省略。。。

<resultMap id="specCategoryValuesMap" type="SpecCategoryValues">
    <id property="id" column="id" />
<result property="categoryId" column="category_id" />
<result property="value" column="value" />
<result property="image" column="image" />
    </resultMap>


中间部分省略,直接上代码

<select id="findListByIds"  resultMap="specCategoryValuesMap">
SELECT 
<include refid="specCategoryValuesColumns"/>
FROM category_values a
<include refid="specCategoryValuesJoins"/>
<where>
a.id in 
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>


当Dao中定义的方法参数为String[]类型为,此处foreach中的collection必须为array,当定义为List时,此处也为list,这里的item="item" 中后者的item为别名,下面的item和后面的index也是同理,值得一提的是这里的返回值记得定义为resultMap。

  1. 2.Dao中的对应的方法定义:

public List<SpecCategoryValues> findListByIds(String[] ids);


此处需要注意String[]数组的定义,必须用这种方式,否则会有提示ids未定义

  1. 3.Service中的对应代码如下:

public List<SpecCategoryValues> findListByIds(String ids){
return specCategoryValuesDao.findListByIds(ids.split(","));
}


好了,到此为止就可以实现了,希望能够帮助大家。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值