• Mybatis Collection查询集合只出现一条数据


    Mybatis Collection查询集合只出现一条数据

    1、原因

        如果两表联查,主表和明细表的主键都是id的话,明细表的多条只能查询出来第一条。

    2、解决办法

        级联查询的时候,主表和从表有一样的字段名的时候,在mysql上命令查询是没问题的。但在mybatis中主从表需要为相同字段名设置别名。设置了别名就OK了。

    例子:

    主表Standard, 从表StandEntity,均有名为id的字段

    <resultMap id="StandardAndEntityResultMap" type="whu.edu.irlab.model.Standard" extends="BaseResultMap">
        <collection property="standEntities" ofType="whu.edu.irlab.model.StandEntity">
            (依据下面的select中更名的字段id别名se_id,在此将相同的字段名改为别名)
            <id column="se_id" property="id" jdbcType="INTEGER" /> 
            <result column="stand_id" property="standId" jdbcType="INTEGER" />
            <result column="stand_name" property="standName" jdbcType="VARCHAR" />
            <result column="entity_name" property="entityName" jdbcType="VARCHAR" />
        </collection>
    </resultMap>
     
    <select id="findAllStandardAndEntity" resultMap="StandardAndEntityResultMap">
        select
        standard.*,
        standard_entity.id se_id,(在此将两表中相同的字段名id改为别名se_id,对应的上面collection部分也需要更改)
        standard_entity.stand_id,
        standard_entity.stand_name,
        standard_entity.entity_name
        from
          standard INNER JOIN standard_entity on standard.id = standard_entity.stand_id
    </select>

    原文链接 http://my.oschina.net/u/1020238/blog/517115?fromerr=qPbx9Vfc

  • 相关阅读:
    Luogu P1131 时态同步
    Codeforces Round #507 B. Shashlik Cooking
    Codeforces Round #507 A. Palindrome Dance
    Luogu P3818 小A和uim之dark♂逃离 Ⅱ
    Luogu P1373 小a和uim之dark♂逃离 Ⅰ
    Luogu P4822 [BJWC2012]冻结
    Luogu P2575 高手过招(博弈论)
    Luogu P1074靶形数独
    Luogu P2323「皇后游戏」
    GodFly的寻宝之旅·状压DP
  • 原文地址:https://www.cnblogs.com/junge/p/5145881.html
Copyright © 2020-2023  润新知