select - Mybatis 关联/集合的嵌套选择不起作用

标签 select nested associations mybatis

我正在尝试使用结果图部分来测试 Mybatis 的用户手册。 Mybatis版本: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 类(class):

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

最佳答案

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

尝试:

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

关于select - Mybatis 关联/集合的嵌套选择不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10545232/

相关文章:

mysql - 选择每项的总和

php - mysql从查询中获取结果

c# - LINQ 根据内部集合值选择

mysql - CakePHP 3.x - 如何通过另一个关联的字段过滤一个关联?

ruby-on-rails - 通过组关系列出属于用户的文档模型的所有实例

php - 我的 WHERE 查询的 MYSQL 部分未运行

json - Hive-从另一个非嵌套的Hive表创建一个嵌套的Hive表

json - 在 SWIFT 4 中解析嵌套的 JSON

c# - 有没有一种好的方法来优化迭代次数相乘的嵌套 for 循环?

UML类图: What is the meaning of attributes at lines?