Does resultMap support return subclasses of a class in Mybatis 3

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Does resultMap support return subclasses of a class in Mybatis 3

庞伟
This is a copy from GitHub:mybatis-3:#1608

Example:

public class Student {
   
private Long id;
   
private String name;
   
private Integer age;
   
//ellipsis  getter and setter method
}

public class StudentExt extends Student {
   
private String no;
     
//ellipsis  getter and setter method
}

public interface StudentMapper {

   
@Select("select id, name, age from student where id= #{id}")
   
Student selectByPrimaryKey(@Param(value = "id") Long id);

   
@Select("select id, name, age from student")
   
List<Student> selectAll();

}

I need a StudentExt, not a Student, coding like this:
// example 1
Student student = studentMapper.selectByPrimaryKey(1L);
StudentExt studentExt = new StudentExt();
BeanUtils.copyProperties(student, studentExt); //Spring BeanUtils

//example 2
List<StudentExt> studentExtList =
        studentMapper
.selectAll().stream().map(student -> {
           
StudentExt studentExt = new StudentExt();
           
BeanUtils.copyProperties(student, studentExt); //Spring BeanUtils
           
return studentExt;
       
}).collect(Collectors.toList());
can support like this:
Mapper class
    @Select("select id, name, age from student where id= #{id}")
   
<T extends Student> T selectByPrimaryKey(@Param(value = "id") Long id, Class<T> clazz);

   
@Select("select id, name, age from student")
   
<T extends Student> List<T> selectAll(Class<T> clazz);

then i can easy coding like this:
StudentExt studentExt = studentMapper.selectByPrimaryKey(1L, StudentExt.class);

List<StudentExt> studentExtList = studentMapper.selectAll(StudentExt.class);

Please reply to me on GitHub, because it's hard to visit Google in China.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/32309411-655c-4f7c-ae20-ca23ddb0af54%40googlegroups.com.