2016-07-21 68 views
0

复合主键我有一个上的两列称为post_locations(POST_ID,LOCATION_ID,LOCATION_NAME,created_ts,updated_ts,CREATED_BY,updated_by)与复合主键POST_ID表,LOCATION_ID。我试图在列上定义id属性,但无法成功。如何定义中的MyBatis映射

我没有找到关于这个主题的文档,请帮我解决这个问题

+0

添加您的创建表语句,请为位置创建单独的故事 –

回答

0

这并不困难。如果你有这样的实体:

public class PostLocations { 

    private PostLocationsPK id; 

    //other properties... 

    //constructor, getters && setters.... 
} 

主键复合:

public class PostLocationsPK { 

    private int postId, locationId; 

    //getters, setters, constructors && equals 
} 

你的映射器应该是这样的:

public interface MapperEntity { 

    @Select("select * from post_locations") 
    @Results({ 
     @Result(id=true, property = "id.postId", column = "post_id"), 
     @Result(id=true, property = "id.locationId", column = "location_id"), 
     //OTHER PROPERTIES... 
     }) 
    public List<PostLocations> findAll(); 

} 

物业PostLocationsPK是ID和PostLocationsPK性能帖子ID和locationID,所以属性是名称属性PK + + 名称PK属性(id.postId和id.locationId)。另一方面,需要添加id = true。