本代码的功能:实现2个查询条件同时满足的循环查询。
将每次要传递的2个参数使用map存储起来,然后再把map存储在List集合中,每次遍历一个map,然后在map中获取要传递的数据。
其实,按照这种思想,可以实现同时满足"N"个查询条件的循环语句。
serviceImpl.java
@Override
public List<NutritionalComposition> findElementWeightByPratiesIdListAndStatusListandPreparationWeightList(List<Integer> pratiesIdList, List<Integer> statusList) {
List<Map<String,Integer>> mapList = new ArrayList<Map<String,Integer>>();
for(int i = 0; i < pratiesIdList.size(); i++){
Map<String,Integer> map = new HashMap<>();
map.put("partiesId", pratiesIdList.get(i));
map.put("status", statusList.get(i));
mapList.add(map);
}
List<NutritionalComposition> list = stoNutritionElementMapper.selectElementWeightByPratiesIdListAndStatusList(mapList);
if(list != null && list.size() > 0){
return list;
}else{
return null;
}
}
mapper.java
List<NutritionalComposition> selectElementWeightByPratiesIdListAndStatusList(@Param("mapList") List<Map<String,Integer>> mapList);
mapper.xml
<select id="selectElementWeightByPratiesIdListAndStatusList" resultType="com.my.en.pojo.custom.NutritionalComposition" parameterType="list" >
select
a.ELEMENT_NAME elementName,
a.UNIT unit,
a.PARTIES_ID partiesId,
a.`STATUS` status,
sum(a.ELEMENT_WEIGHT * (b.NUTRITION_WEIGHT / 100)) elementWeight
from
(
select
sne.ELEMENT_NAME,
sne.NUTRITION_ID,
sne.ELEMENT_ID,
sne.ELEMENT_WEIGHT,
sne.UNIT,
rpd.PARTIES_ID,
rpd.`STATUS` status
from
sto_nutrition_element sne
left join
rec_parties_details rpd
on
rpd.NUTRITION_ID = sne.NUTRITION_ID
<where>
sne.DELETE_MARK = 0
<if test="mapList != null">
<foreach collection="mapList" item="map" open=" and (" close=")"
separator="or">
(rpd.PARTIES_ID = #{map.partiesId} and rpd.`STATUS` = #{map.status})
</foreach>
</if>
</where>
ORDER BY `STATUS`
)a
left join
(
select
NUTRITION_ID,
NUTRITION_WEIGHT,
`STATUS`
from
rec_parties_details
<where>
<if test="mapList != null">
<foreach collection="mapList" item="map" open=" and (" close=")"
separator="or">
(PARTIES_ID = #{map.partiesId} and `STATUS` = #{map.status})
</foreach>
</if>
</where>
)b
on
a.NUTRITION_ID = b.NUTRITION_ID and a.`STATUS` = b.`STATUS`
GROUP BY
a.ELEMENT_ID,
a.PARTIES_ID,
a.`STATUS`
ORDER BY
a.`STATUS`,
a.ELEMENT_ID
</select>