0

I want to map a nested object with one select query.

For example:

// parent class
public class Parent {
    private String name;
    private Child child;
}
    
// child class
public class Child {
    private name;
}

And I want to get whole parent class with one select query.

For example:

SELECT P.NAME, C.NAME from PARENT P, CHILD C

This is just example, please ignore specific grammar.

Is this possible?

1
  • 1
    It's possible, but you have to assign different column alias for C.NAME e.g. SELECT P.NAME, C.NAME C_NAME from .... For mapping, see the doc. It's explained as "nested results". Commented Apr 26, 2022 at 6:46

1 Answer 1

1

In your Main

@MappedTypes(Parent.class)
@MapperScan("com.foo.mybatis.batis.mapper")
@SpringBootApplication
public class BatisApplication {...}

@MapperScan value should locate to your mapper package.

Use this in your interface. @Mapper @Select

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ParentMapper {

    @Select("select * from parents")
    List<Parent> findAll();
}

You can add your custom query in @Select("SELECT P.NAME, C.NAME from PARENT P, CHILD C")

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your kindness. But this is what i wanted. What i want is, single query for data, split and save them other objects. Anyway, Thanks.
Checkout this @sb0321

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.