天天看點

[Mybatis-Plus] 調用自帶方法 報錯 Invalid bound statement

在調用Mybatis-Plus(版本:2.1-gamma)的自帶方法 selectById 時,報錯 Invalid bound statement:

PlaceType hasPlaceType = placeTypeMapper.selectById(id);
           

其中placeTypeMapper對應的實體類為 PlaceType:

public interface PlaceTypeMapper extends BaseMapper<PlaceType> {
}
           

經檢查,在實體類中,不對主鍵字段添加 @TableId("資料庫字段名稱") 注解的話,在調用涉及id的自帶方法時就出現此錯誤。估計是mybatis-plus無法識别主鍵字段。

實體類:

@TableName("_place_type")//資料庫表名稱
public class PlaceType {

    @TableId("place_type_id")//資料庫主鍵名稱
    private Integer placeTypeId;

    private String name;//資料庫字段
    private Integer isDeleted;//資料庫字段
    //.....
}
           

是以,建議如果使用Mybatis-Plus的話,最好在實體類中詳細注解好表名稱(@TableName)、表主鍵(@TableId),以免出現這種很難找到原因的錯誤。

另:據說新版本mybatis-plus已經解決此問題

參考
id相關的操作都會報Invalid bound statement這個異常