2012-05-11 39 views
1

我想用結果地圖部分來測試Mybatis的用戶手冊。 MyBatis的版本:MyBatis的-3.1.0Mybatis嵌套選擇關聯/收藏不起作用

<setting name="lazyLoadingEnabled" value="false" /> 


<resultMap id="blogMap" type="blog"> 
<constructor> 
    <idArg column="id" javaType="_long" /> 
    <arg column="title" javaType="String" /> 
</constructor> 
<association property="author" javaType="author" column = "author_id"   select = "getAuthor"/> 
</resultMap> 

<select id="getBlog" parameterType="Long" resultMap="blogMap"> 
    select 
    b.id, 
    b.title 
    from 
    blog b 
    where b.id = #{id} 
</select> 

<select id="getAuthor" parameterType="Long" resultType="author"> 
    select 
    a.id , 
    a.username, 
    a.password 
    from author a 
where a.id = #{id} 
</select> 

我的Java類:

public class Blog { 
private long id; 
private String title; 

private Author author; 
private List<Post> posts; 
     //getter, setters and the constructor 

public class Author { 
private long id; 
private String username; 
private String password; 
private String email; 
private String bio; 
private String favouriteSection; 

最後,我的測試模塊

BlogMapperInterface bm = context.getBean("blogMapper",BlogMapperInterface.class); 
    Blog b = bm.getBlog(1); 

調試堆棧跟蹤

[10/05/12 06:45:19:019 SGT] DEBUG datasource.DataSourceUtils: Fetching JDBC Connection from DataSource 
[10/05/12 06:45:19:019 SGT] DEBUG BlogMapperInterface.getBlog: ooo Using Connection  [jdbc:oracle:thin:@*, UserName=*, Oracle JDBC driver] 
[10/05/12 06:45:19:019 SGT] DEBUG BlogMapperInterface.getBlog: ==> Preparing: select b.id, b.title from blog b where b.id = ? 
[10/05/12 06:45:19:019 SGT] DEBUG BlogMapperInterface.getBlog: ==> Parameters: 1(Long) 
[10/05/12 06:45:19:019 SGT] DEBUG BlogMapperInterface.getBlog: <== Columns: ID, TITLE 
[10/05/12 06:45:19:019 SGT] DEBUG BlogMapperInterface.getBlog: <==  Row: 1, first blog 
[10/05/12 06:45:19:019 SGT] DEBUG datasource.DataSourceUtils: Returning JDBC Connection to DataSource 

爲什麼不調用getAuthor?每當我打電話給getBlog()時,不應該調用它嗎?

回答

1

因爲沒有在結果從getBlog有一個AUTHOR_ID列。

嘗試:

select 
    b.id, 
    b.title, 
    b.author_id 
    from 
    blog b 
    where b.id = #{id}