Mybatis Nested Select for Association/Collection 不起作用 [英] Mybatis Nested Select for Association/Collection doesn't work

查看:31
本文介绍了Mybatis Nested Select for Association/Collection 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用结果映射部分测试 Mybatis 的用户手册.Mybatis 版本:mybatis-3.1.0

I am trying to test out Mybatis's user manual with the result map section. Mybatis version : mybatis-3.1.0

<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() 时不应该调用它吗?

Why the getAuthor is not invoked? Shouldn't it be invoked whenever I call getBlog()?

推荐答案

因为从 getBlog 得到的结果中没有 author_id 列.

Because there is not an author_id column in the result got from getBlog.

试试:

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

这篇关于Mybatis Nested Select for Association/Collection 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆