1.mapper的配置
public interface TbCourseMapper {
List<TbCourse> getWithIds(@Param("params") Map<String,Object> map);
}
2.mapper.xml文件中的写法
<select id="getWithIds" resultType="com.certificate.course_center.po.TbCourse"
parameterType="Map">
SELECT *
FROM tb_course tc
WHERE course_id in
<foreach collection="params.ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
3.service中的写法
public List<TbCourse> getCourseWithCourseIds(List<Integer> courseIds) {
Map<String,Object> map=new HashMap<>();
map.put("ids",courseIds);
return tbCourseMapper.getWithIds(map);
}
至此在sql中遍历的列表的功能就完成了
本文介绍了一种使用MyBatis进行批量课程ID查询的方法。具体包括:定义Mapper接口及XML配置实现对多个课程ID的SQL遍历查询,以及Service层的实现方式。
448

被折叠的 条评论
为什么被折叠?



