我使用mybatis查询嵌套对象
我的豆,Goods.java
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 here...
}
GoodsImg.java
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 ...
}
两个结果图
<!-- 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" column="goods_id" 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>
我的 sql
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 limit 10;
mysql中sql的结果
下面的第一列是“goods_id”
1 test_name 1 1 更新 desc 1 img1 tttt 1
1 test_name 1 1 更新 desc 1 img2 ttt 2
1 test_name 1 1 更新 desc 1 img3 ttt 3
2 test_name2 2 2 测试 desc2 0 df ttt 3
1 test_name 1 1 更新 desc 1 dfdf ddd 2
3 3333 3 3 ddfd 1 dfd 萨德夫 1
1 test_name 1 1 更新 desc 1 sdfsd sdf 2
java中的查询结果是:
列表 [[Goods(id=1) with 5 GoodsImg], [Goods(id=2) with 5 GoodsImg], [Goods(id=3) with 5 GoodsImg]]
此列表包含 3 件相同的商品
正确的列表应该是:
列表 [[Goods(id=1) with 5 GoodsImg], [Goods(id=2) with 1 GoodsImg], [Goods(id=1) with 1 GoodsImg]]
并且,如果我像这样更改 mysql 中的数据顺序:
1 test_name 1 1 更新 desc 1 img1 tttt 1
1 test_name 1 1 更新 desc 1 img2 ttt 2
1 test_name 1 1 更新 desc 1 img3 ttt 3
2 test_name2 2 2 测试 desc2 0 df ttt 3
1 test_name 1 1 更新 desc 1 dfdf ddd 2
1 test_name 1 1 更新 desc 1 sdfsd sdf 2
3 3333 3 3 ddfd 1 dfd 萨德夫 1
那么java中的查询结果是:
列表 [[Goods(id=1) with 5 GoodsImg], [Goods(id=1) with 5 GoodsImg], [Goods(id=3) with 1 GoodsImg]]
仍然是错误的列表,没有商品(id=2)
如果我像这样更改mysql中的数据顺序:
1 test_name 1 1 更新 desc 1 img1 tttt 1
1 test_name 1 1 更新 desc 1 img2 ttt 2
1 test_name 1 1 更新 desc 1 img3 ttt 3
1 test_name 1 1 更新 desc 1 dfdf ddd 2
1 test_name 1 1 更新 desc 1 sdfsd sdf 2
2 test_name2 2 2 测试 desc2 0 df ttt 3
3 3333 3 3 ddfd 1 dfd 萨德夫 1
那么java中的查询结果就可以了
由于我无法控制mysql中的数据顺序,所以总是会返回错误的列表结果
那么,问题出在哪里,这是 mybatis 的错误吗?我的
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
提前谢谢