码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-05-05 17:23:10      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:and   join   ase   select   src   从表   问题   base   extend   

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>

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

标签:and   join   ase   select   src   从表   问题   base   extend   

原文地址:http://www.cnblogs.com/liuyun-10/p/6813712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!