Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
DeooK Avatar asked Feb 28 '26 07:02

DeooK


1 Answers

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"),
})
like image 185
DeooK Avatar answered Mar 02 '26 13:03

DeooK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!