天天看点

[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这个异常