I have tables event
and message
, which have one-to-one relationship and are represented by classes:
public class Event {
private Long id;
private Message message;
}
public class Message {
private Long id;
}
And there are two mappers: EventMapper
and MessageMapper
:
public interface EventMapper {
@Select("SELECT * FROM event WHERE date BETWEEN #{from} AND #{to}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "message", column = "message_id",
javaType = Message.class, one = @One(select = "getMessage"))
})
List<Event> selectInInterval(@Param("from") Date from, @Param("to") Date to);
@Select("SELECT * FROM message WHERE id = #{id}")
Message getMessage(@Param("id") Long id);
}
public interface MessageMapper {
@Select("SELECT * FROM message WHERE id = #{id}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "text", column = "text")
})
List<Event> selectById(@Param("id") Long id);
}
EventMapper
selects events and maps them via an annotation, using getMessage
to select and map event's message. But I already have identical method selectById
in MessageMapper
, so is it possible to reuse it?
In order to use any definition from the other mapper including a statement or ResultMap
you should include namespace
(fully qualified name) of the mapper into the identifier of that entity.
In your example assuming that MessageMapper
is in com.company.app.mappers
package it would be:
public interface EventMapper {
@Select("SELECT * FROM event WHERE date BETWEEN #{from} AND #{to}")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "message", column = "message_id",
javaType = Message.class,
one = @One(select = "com.company.app.mappers.MessageMapper.getMessage"))
})
List<Event> selectInInterval(@Param("from") Date from, @Param("to") Date to);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.