@ResponseBody
public ResponseEntity<Map<String, Object>> paramProduct(String fieldListStr, String spuCode) {
Map<String, Object> returnMap = new HashMap<>();
try {
Map<String, Object> paramMap = new HashMap<>();
if (StringUtils.isBlank(fieldListStr) || StringUtils.isBlank(spuCode)) {
returnMap.put("status", 0);
returnMap.put("msg", "请求参数不合法");
return new ResponseEntity(returnMap, HttpStatus.OK);
}
List<String> fieldList = Arrays.asList(fieldListStr.split(","));
paramMap.put("fieldList", fieldList);
paramMap.put("spuCode", "'" + spuCode + "'");
Map<String, Object> resultMap = productManageService.paramProduct(paramMap);
returnMap.put("status", 1);
returnMap.put("msg", "success");
returnMap.put("data", resultMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
returnMap.put("status", 0);
returnMap.put("msg", "系统异常");
}
return new ResponseEntity(returnMap, HttpStatus.OK);
}
controller中fieldListStr是需要查询的字段,spuCode是判断条件,需要注意的是spuCode需要用单引号括起来,否则查询会报错!
<select id="paramProduct" resultType="Map" parameterType="java.util.Map" statementType="STATEMENT" >
select
<foreach collection="fieldList" item="field" separator=",">
${field}
</foreach>
from table where 1=1
<if test="spuCode != null and spuCode != ''" >
and spu_code = ${spuCode}
</if>
</select>
对应的脚本写法,注意statementType="STATEMENT"这个需要配置,还是对应参数获取不适用#,需要换为$
mybatis中,如果有需要根据某个list数组中的顺序进行排序的话,可以如下:
<select id="queryProductListOrder" resultMap="BaseResultMap" parameterType="java.util.Map">
select pro_id,name,price,cover,unit,clicks,cut_price,cut_unit,card_price, card_ship_type
from sb_product_statistics
where 1=1
<if test="prdIdList != null and prdIdList.size > 0" >
and pro_id in
<foreach collection="prdIdList" index="index" item="productId" open="(" separator="," close=")">
#{productId}
</foreach>
order by field(pro_id,<foreach collection="prdIdList" index="index" item="productId" separator="," >
#{productId}
</foreach>)
</if>
</select>
