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>

转载于:https://www.cnblogs.com/liuyun-10/p/6813712.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值