mybatis assocation只返回一个大小为1的列表?

时间:2016-04-30 07:18:48

标签: java mybatis

下面是我的mapper xml。

<mapper namespace="/">
    <resultMap id="JiKeAuthorMap" type="Author">
        <id property="id" column="author.id" />
        <result property="realName" column="realName" />
        <result property="IDCard" column="IDCard" />
        <association property="jikeUser" column="userID"
            javaType="JiKeUser" >
            <id property="id" column="jikeUser.id" />
            <result property="userName" column="userName" />
            <result property="password" column="password" />
        </association>

    </resultMap>

    <select id="selectAuthorJoin" resultMap="JiKeAuthorMap">
        select * from author   join jikeUser 
            on jikeuser.id=author.userID
    </select>
</mapper>

下面是作者

SELECT * FROM author;

    id  realName      userID  IDCard  
------  ------------  ------  --------
     3  cccccc             3  (NULL)  
     2  bbb                2  (NULL)  
     1  dddddd             8  (NULL)  

下表是 JiKeUser

SELECT * FROM JiKeUser;

    id  userName   password  
------  ---------  ----------
     2  jike00100  666666    
     3  jike00200  888888    
     8  author001  123456    

下面是java pojo的主要代码。(作者引用了jikeUser)

public class Author {
    private Integer id; // 
    private JiKeUser jikeUser; //reference to JiKeUser table

然后我使用下面的代码来执行&#39; select&#39;。尺寸只有一个。

    List<Author> ap=session.selectList("selectAuthorJoin"); 
    System.out.println(ap.size());   //1

但实际上大小应该是三。你可以在下面看到。

SELECT * FROM author INNER JOIN jikeUser 
            ON jikeuser.id=author.userID ;
    id  realName  userID  IDCard      id  userName   password  
------  --------  ------  ------  ------  ---------  ----------
     2  bbb            2  (NULL)       2  jike00100  666666    
     3  cccccc         3  (NULL)       3  jike00200  888888    
     1  dddddd         8  (NULL)       8  author001  123456    

但是我的代码只返回第一行,如下所示。

 id  realName  userID  IDCard      id  userName   password  
    ------  --------  ------  ------  ------  ---------  ----------
         2  bbb            2  (NULL)       2  jike00100  666666    

1 个答案:

答案 0 :(得分:0)

尝试替换

<id property="id" column="author.id" />

<result property="id" column="author.id" />

查看MyBatis collection in association only return one row

它对我有用。