狀況描述:當使用JOIN查詢,若是SQL查詢出來的記錄不是按id列排序的,則生成的List結果會有問題html
案例:java
1) 數據庫模型sql
簡而言之一個Goods包含多個Goods_Img數據庫
2) Java Beanapp
Goods.javacode
public class Goods { private Integer goodsId; private String goodsName; private Integer goodsStorageNum; private Integer goodsScore; private GoodsStatus goodsStatus; private String goodsDescription; private List<GoodsImg> goodsImgList; ... getter and setter ... }
GoodImg.javaxml
public class GoodsImg { private Integer imgId; private Integer goodsId; private String imgDir; private Integer imgSize; private String imgName; ... getter and setter ... }
3) Mapperhtm
<!-- Result Map --> <!-- goods resultmap --> <resultMap id="goodsResultMap" type="com.qunar.scoresystem.bean.Goods"> <id property="goodsId" column="goods_id" /> <result property="goodsName" column="goods_name" /> <result property="goodsStorageNum" column="goods_storage_num" /> <result property="goodsScore" column="goods_score" /> <result property="goodsDescription" column="goods_description" /> <result property="goodsStatus" column="goods_status" /> <collection property="goodsImgList" resultMap="goodsImgResult" /> </resultMap> <!-- goodsimage resultmap --> <resultMap id="goodsImgResult" type="com.qunar.scoresystem.bean.GoodsImg"> <id property="imgId" column="img_id" /> <result property="goodsId" column="goods_id" /> <result property="imgDir" column="img_dir" /> <result property="imgSize" column="img_size" /> <result property="imgName" column="img_name" /> </resultMap>
4) 執行的 SQLblog
select goods.goods_id as goods_id, goods.goods_name as goods_name, goods.goods_storage_num as goods_storage_num, goods.goods_score as goods_score, goods.goods_description as goods_description, goods.goods_status as goods_status , goods_img.img_name as img_name , goods_img.img_dir as img_dir , goods_img.img_size as img_size from goods join goods_img on goods.goods_id=goods_img.goods_id
原文地址:http://www.cnblogs.com/zemliu/archive/2013/08/16/3263053.html排序