- 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。
- 2.Dao中的对应的方法定义:
public List<SpecCategoryValues> findListByIds(String[] ids);
此处需要注意String[]数组的定义,必须用这种方式,否则会有提示ids未定义
- 3.Service中的对应代码如下:
public List<SpecCategoryValues> findListByIds(String ids){ return specCategoryValuesDao.findListByIds(ids.split(",")); }
好了,到此为止就可以实现了,希望能够帮助大家。