MyBatis:如何映射嵌套对象

问题描述 投票:0回答:1

我想避免在我的项目中使用xml文件并仅使用注释。我不明白的是如何使用MyBatis 3.5映射嵌套对象。

我有这样的POJO

    public class Father {
            private String name;
            private int age;
            private Son son;
    }

    public class Son {
            private String name;
            private int age;
    }

如何在没有xml文件的情况下映射名称和年龄属性?使用@Results和@Result我可以映射父亲属性,但我不能使用嵌套注释。

mybatis spring-mybatis
1个回答
0
投票

我找到了解决方案:MyBatis可以使用点访问@Result注释中的嵌套对象:

@Select([...])
@Results(value = {
    @Result(property = "name", column = "name_db_colum"),
    @Result(property = "age", column = "age_db_colum"),
    @Result(property = "son.name", column = "son_name_db_colum"),
    @Result(property = "son.age", column = "son_age_db_colum"),
})
© www.soinside.com 2019 - 2024. All rights reserved.