MyBatis insert数据返回主键ID

1、dao 层添加方法不要加 @Param("")  注解

2、xml 添加  useGeneratedKeys="true" keyProperty="id"   keyProperty="id"对应主键名称


model

public class ProjectRole implements Serializable {

    /**
     * 主键
     */
    private int id;
    
    /**
     * 角色名称
     */
    private String roleName;
    
    /**
     * 备注
     */
    private String remark;
    
    /**
     * 项目ID
     */
    private int projectId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }
}


dao

public void addProjectRole(ProjectRole projectRole);


xml

 useGeneratedKeys="true" keyProperty="id" >
    INSERT INTO project_role (role_name ,remark ,project_id) 
        VALUES (#{roleName},#{remark},#{projectId});

你可能感兴趣的:(MyBatis insert数据返回主键ID)