Mybatis选择带有嵌套对象 [英] Mybatis select with nested objects

查看:1434
本文介绍了Mybatis选择带有嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MyBatis进行简单选择.

I am using MyBatis to do a simple select.

假设我们有以下课程:

class Book {
    private String bookName;

    public Book(String bookName){
        this.bookName = bookName;
    }

    public String getBookName(){
        return bookName;
    }
}


class Student {
    private String studentName;
    private Book book;

    public Student(){}

    // getters and setters
}

我在返回Student对象的方法上具有注释.

I have an annotation on a method that returns a Student object.

@Select("Select studentName, book from Students")

我的问题是这本书总是空的.我假设MyBatis将使用该JDBC类型(在这种情况下为String)调用构造函数来填充书籍.我缺少什么或做错了什么?

My Issue is that book is always null. I was under the assumption MyBatis will call the constructor with that JDBC type (in this case String) to populate book. What am I missing or doing incorrectly?

推荐答案

一个选项是

使用@ConstructorArgs批注显式调用Constructor方法.

Use @ConstructorArgs annotations to explicitly call Constructor method.

@Select("Select studentName, book from Students")
@ConstructorArgs(value = { 
@Arg(column = "studentName", javaType=java.lang.String.class),
@Arg(column = "book", javaType = java.lang.String.class)
})

并将其传递给Student构造函数,该构造函数将调用Book构造函数.

and pass them to Student constructor, which calls Book constructor.

这篇关于Mybatis选择带有嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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