2016-07-21 123 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。