DeooK
DeooK

Reputation: 545

MyBatis: how to map nested objecs

I want to avoid xml files in my project and use only annotations. What I'm not understand is how to map nested object with MyBatis 3.5.

I have a POJO like this

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

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

How can I map name and age properties without xml files? With @Results and @Result I can map father propery but I cannot use nested annotations.

Upvotes: 3

Views: 1439

Answers (1)

DeooK
DeooK

Reputation: 545

I found the solution: MyBatis can access nested object in @Result annotation using the dot:

@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"),
})

Upvotes: 2

Related Questions