复合主键我有一个上的两列称为post_locations(POST_ID,LOCATION_ID,LOCATION_NAME,created_ts,updated_ts,CREATED_BY,updated_by)与复合主键POST_ID表,LOCATION_ID。我试图在列上定义id属性,但无法成功。如何定义中的MyBatis映射
我没有找到关于这个主题的文档,请帮我解决这个问题
复合主键我有一个上的两列称为post_locations(POST_ID,LOCATION_ID,LOCATION_NAME,created_ts,updated_ts,CREATED_BY,updated_by)与复合主键POST_ID表,LOCATION_ID。我试图在列上定义id属性,但无法成功。如何定义中的MyBatis映射
我没有找到关于这个主题的文档,请帮我解决这个问题
这并不困难。如果你有这样的实体:
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。
搜索这个问题,我碰到过这样的:http://mybatis-user.963551.n3.nabble.com/Composite-Keys-in-Resultmaps-td2775742.html
我希望这可以帮助,如果不尝试提供更多的细节。
添加您的创建表语句,请为位置创建单独的故事 –