在自已做的内容中通过获取多个商品Id,以此来获取多个商品详细信息。但数据库返回的商品信息的顺序与原来List中产品的id顺序并不匹配,这就导致了前端页面商品的信息显示混乱,不匹配。
通过网上找到order by排序可以有效解决这个问题,返回与查询时的产品id顺序一致。还有一点,List时有序的
select prod_id, prod_name, price, `describe`, prod_date, prod_pic, integral, Prod_detail from product_info
where prod_id in('sayx0420','smxj0420','flpd0420','snxj0420' )
order by
instr(',sayx0420,smxj0420,flpd0420,snxj0420',CONCAT(',',prod_id,','))
select <foreach collection="list" open="'," close=",'" separator="," item="prodId" >
${prodId}
</foreach>
因为空格,回车与换行的原因这样达不到下面代码的效果instr(’,sayx0420,smxj0420,flpd0420,snxj0420’,CONCAT(’,’,prod_id,’,’))
思路:替换图片内的回车,换行与空格为单引号
ASCII码
char(10) 换行
char(13) 回车
解决:用sql的replace方法去替换
select replace(replace(replace(
${prodId}
,char(13),''),char(10),''),' ' ,'')
<select id="selectByProdIds" resultMap="BaseResultMap" parameterType="java.util.List">
select prod_id, prod_name, price, `describe`, prod_date, prod_pic, integral, Prod_detail
from product_info
where prod_id
<foreach collection="list" separator="," open="in(" close=")" item="prodId">
#{prodId}
</foreach>
order by
instr(replace(replace(replace(
<foreach collection="list" open="'," close=",'" separator="," item="prodId" >
${prodId}
</foreach>,char(13),''),char(10),''),' ' ,''),CONCAT(',',`prod_id`,','))
</select>