需求:从steam_wd_order表中取出item_id(是个list),然后在表edu_course_series中取出item_id在item_id(list)中的所有数据,下面是三种方式。
API调用层:
//weiDianOrderDtoList.stream():把数据编程数据流;.map:循环数据流;item->item.getItem_id():用变量item接收循环出来的对象,再取出item_id
//.collect:将数据转成集合;Collectors.toList():将集合类型转为list类型
List<String> itemIdList = weiDianOrderDtoList.stream().map(item->item.getItem_id()).collect(Collectors.toList());
//根据itemID获取serises(第一种方式)
List<CourseSeriesDto> courseSeriesDtos = weidianBiz.getCourseSeriesByItemId(itemIdList);
logger.info(courseSeriesDtos);
//根据itemID获取serises(第二种方式)
String items=StringUtils.join(itemIdList, ",");
List<CourseSeriesDto> courseSeriesDtos2 = weidianBiz.getCourseSeries2ByItemId(items);
logger.info(courseSeriesDtos2);
//根据itemID获取serises(第三种方式)
List<CourseSeriesDto> courseSeriesDtos3 = weidianBiz.getCourseSeries3ByItemId(iphone);
logger.info(courseSeriesDtos3);
dao层:
//获取商品对应的课程(第一种方式)
public List<CourseSeriesDto> getCourseSeriesByItemId(@Param("itemIdList")List<String> itemIdList);
//获取商品对应的课程(第二种方式)
public List<CourseSeriesDto> getCourseSeries2ByItemId(@Param("itemIds")String itemIds);
//获取商品对应的课程(第三种方式)
public List<CourseSeriesDto> getCourseSeries3ByItemId(@Param("iphone")String iphone);
xml层:
<!-- 根据item查询课程 第一种方式 -->
<!--SELECT * FROM edu_course_series WHERE item_id=#{item_id} AND deleted=1-->
<select id="getCourseSeriesByItemId" resultType="com.haiziyouke.service.dto.CourseSeriesDto">
select * from edu_course_series where item_id in
<foreach item="item" index="index" collection="itemIdList" open="(" separator="," close=")">
#{item}
</foreach>
AND deleted=1
</select>
<!-- 根据item查询课程 第二种方式 -->
<!--SELECT * FROM edu_course_series WHERE item_id=#{item_id} AND deleted=1-->
<select id="getCourseSeries2ByItemId" resultType="com.haiziyouke.service.dto.CourseSeriesDto">
select * from edu_course_series where item_id in (#{itemIds}) AND deleted=1
</select>
<!-- 根据item查询课程 第s三种方式 -->
<!--SELECT * FROM edu_course_series WHERE item_id=#{item_id} AND deleted=1-->
<select id="getCourseSeries3ByItemId" resultType="com.haiziyouke.service.dto.CourseSeriesDto">
select * from edu_course_series where item_id in (SELECT item_id FROM steam_wd_order WHERE user_phone=#{iphone} OR phone=#{iphone}) AND deleted=1
</select>