MyBatis嵌套集合查询导致重复的错误结果

时间:2013-08-15 16:30:02

标签: java collections mybatis ibatis

我使用mybatis查询嵌套对象

我的bean,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 update desc 1 img1 tttt 1

1 test_name 1 1 update desc 1 img2 ttt 2

1 test_name 1 1 update desc 1 img3 ttt 3

2 test_name2 2 2 test desc2 0 df ttt 3

1 test_name 1 1 update desc 1 dfdf ddd 2

3 3333 3 3 ddfd 1 dfd sadf 1

1 test_name 1 1 update 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)5 GoodsImg],[Goods(id = 2)with 1 GoodsImg],[Goods(id = 1)with 1 GoodsImg]]

并且,如果我像这样更改mysql中的数据顺序:


1 test_name 1 1 update desc 1 img1 tttt 1

1 test_name 1 1 update desc 1 img2 ttt 2

1 test_name 1 1 update desc 1 img3 ttt 3

2 test_name2 2 2 test desc2 0 df ttt 3

1 test_name 1 1 update desc 1 dfdf ddd 2

1 test_name 1 1 update desc 1 sdfsd sdf 2

3 3333 3 3 ddfd 1 dfd sadf 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 update desc 1 img1 tttt 1

1 test_name 1 1 update desc 1 img2 ttt 2

1 test_name 1 1 update desc 1 img3 ttt 3

1 test_name 1 1 update desc 1 dfdf ddd 2

1 test_name 1 1 update desc 1 sdfsd sdf 2

2 test_name2 2 2 test desc2 0 df ttt 3

3 3333 3 3 ddfd 1 dfd sadf 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>

提前谢谢

2 个答案:

答案 0 :(得分:0)

将select语句中的join修改为left join将起作用。

答案 1 :(得分:0)