mybatis association 关联查询只返回一条记录

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

造成以上情况可能的原因:

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

例子:

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

 1 <resultMap id="StandardAndEntityResultMap" type="whu.edu.irlab.model.Standard" extends="BaseResultMap">
 2     <collection property="standEntities" ofType="whu.edu.irlab.model.StandEntity">
 3         (依据下面的select中更名的字段id别名se_id,在此将相同的字段名改为别名)
 4         <id column="se_id" property="id" jdbcType="INTEGER" /> 
 5         <result column="stand_id" property="standId" jdbcType="INTEGER" />
 6         <result column="stand_name" property="standName" jdbcType="VARCHAR" />
 7         <result column="entity_name" property="entityName" jdbcType="VARCHAR" />
 8     </collection>
 9 </resultMap>
10  
11 <select id="findAllStandardAndEntity" resultMap="StandardAndEntityResultMap">
12     select
13     standard.*,
14     standard_entity.id se_id,(在此将两表中相同的字段名id改为别名se_id,对应的上面collection部分也需要更改)
15     standard_entity.stand_id,
16     standard_entity.stand_name,
17     standard_entity.entity_name
18     from
19       standard INNER JOIN standard_entity on standard.id = standard_entity.stand_id
20 </select>

  2、一对多不能用Association,要用Collection。

  根据经验,使用association这个元素很容易出错,建议在resultMap中先换一种写法,不要用association。修改测试一下,如果成功的话,就基本可以去顶是association的问题了,之后查一下association详细资料,应该能解决。如果不是association的问题,就调查一下配置文件等等,总能够解决的。

  3、resultMap配置有问题

  bean代码如下:

 1 public class QueryVO {
 2     
 3     private AppUser appuser;
 4     
 5     private Tb tb;
 6 
 7     public AppUser getAppuser() {
 8         return appuser;
 9     }
10 
11     public void setAppuser(AppUser appuser) {
12         this.appuser = appuser;
13     }
14 
15     public Tb getTb() {
16         return tb;
17     }
18 
19     public void setTb(Tb tb) {
20         this.tb = tb;
21     }
22 }

mapper.xml配置:

 1     <select id="getItemsList" parameterType="QueryVO" resultMap="items">
 2         SELECT
 3         xpro_sys_tb.*,
 4         xpro_sys_app_user.*
 5         FROM
 6         xpro_sys_tb,
 7         xpro_sys_app_user
 8         WHERE xpro_sys_tb.author_id =
 9         xpro_sys_app_user.USER_ID
10         <include refid="query_items_where"></include>
11     </select>
12 
13     <resultMap type="QueryVO" id="items">
14         <association property="appuser" resultMap="appUser"></association>
15         <association property="tb" resultMap="tb"></association>
16     </resultMap>

以上代码导致的结果是查询出来的总是最后一条数据,类似后面数据覆盖了之前数据的现象。

发现问题的关键在于resultMap中如果不定义类似主键之类的能够区分每一条结果集的字段的话,会引起后面一条数据覆盖前面一条数据的现象。

最终将resultMap中添加id,并相应在bean中添加该字段,代码如下,问题解决。

 1 public class QueryVO {
 2     
 3     private Integer id;
 4     
 5     private AppUser appuser;
 6     
 7     private Tb tb;
 8 
 9     public AppUser getAppuser() {
10         return appuser;
11     }
12 
13     public void setAppuser(AppUser appuser) {
14         this.appuser = appuser;
15     }
16 
17     public Tb getTb() {
18         return tb;
19     }
20 
21     public void setTb(Tb tb) {
22         this.tb = tb;
23     }
24 
25     public Integer getId() {
26         return id;
27     }
28 
29     public void setId(Integer id) {
30         this.id = id;
31     }
32     
33     @Override
34     public String toString() {
35         return "QueryVO [id=" + id + ", appuser=" + appuser + ", tb=" + tb + "]";
36     }
37 }

mapper.xml代码:

 1     <select id="getItemsList" parameterType="QueryVO" resultMap="items">
 2         SELECT
 3         xpro_sys_tb.*,
 4         xpro_sys_app_user.*
 5         FROM
 6         xpro_sys_tb,
 7         xpro_sys_app_user
 8         WHERE xpro_sys_tb.author_id =
 9         xpro_sys_app_user.USER_ID
10         <include refid="query_items_where"></include>
11     </select>
12 
13     <resultMap type="QueryVO" id="items">
14         <id property="id" column="id"/>
15         <association property="appuser" resultMap="appUser"></association>
16         <association property="tb" resultMap="tb"></association>
17     </resultMap>
原文地址:https://www.cnblogs.com/link1988/p/5464854.html