mybatis主键自增长,注解返回主键

实体类

 @TableId(value = "projectID",type = IdType.AUTO)
    @TableField("projectID")
    private Long projectID;

插入的时候就不能直接用BaseMapper接口的Integer insert(T var1);了,需要自己实现

@Options(useGeneratedKeys = true, keyProperty = "projectID", keyColumn = "projectID")
    @Insert("insert into "+tableName+"(projectName,creTm,createrID,createrName,finUpdaterID,finUpdateTm,mangerName,mangerID,projectContent,participantIDstr) VALUES(#{projectName},#{creTm},#{createrID},#{createrName},#{finUpdaterID},#{finUpdateTm},#{mangerName},#{mangerID},#{projectContent},#{participantIDstr})")
    int addCmpProject(CmpProject project);

需要用以上两个注解实现,如此

Integer insert = projectMapper.addCmpProject(project);

执行之后的project中就已经包含主键了

你可能感兴趣的:(mybatis)