簡體   English   中英

如何在mybatis映射中定義復合主鍵

[英]how to define composite primary key in mybatis mapping

我有一個名為post_locations(post_id,location_id,location_name,created_ts,updated_ts,created_by,updated_by)的在兩列post_id,location_id上有復合主鍵。 我嘗試在兩列上都定義id屬性,但無法成功。

我沒有找到有關此主題的任何文檔,請幫助我解決此問題

在搜索此問題時,我遇到了以下問題: http : //mybatis-user.963551.n3.nabble.com/Composite-Keys-in-Resultmaps-td2775742.html

我希望這可以幫助您,如果不能嘗試提供更多詳細信息。

這並不困難。 如果您有這樣的實體:

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中的屬性是postID和locationID,因此這些屬性是名稱屬性PK + + 名稱PK屬性 (id.postId和id.locationId)。 另一方面,需要添加id = true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM