使用AutoGenerator(AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码)生成代码后,在Controller中使用Service的getById()进行查询,返回的对象居然是null
问题出在Entity上,由于我数据表的id字段名为id且int类型,自动生成的id字段名serialVersionUID且类型为long类型,与数据库表对不上号。
使用getById(id)查询自然为null
因为entity生成出来的主键没注意到是不符合数据表的
所以解决方法是,把主键改成数据表对应的:把主键id字段名改回id,且数据类型设置int
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("board_parent")
public class Parent extends BaseEntity {
//private static final long serialVersionUID = 1L;
private int id;
@NotEmpty(message = "父论坛名不能为空!")
private String name;
@NotNull(message = "管理员ID不能为空!")
private Integer adminId;
public LocalDateTime createdDate;
public LocalDateTime updateDate;
public Integer state;
}