问题:继承BaseMapeer采用自带的insert之后,返回的是插入成功的条数,无法获得自增主键的ID
=======MAPPER=======
@Mapper
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}====Entity=========
public class Student {private Integer id;private String name;private Integer age;}
=======Service=======
@Service
public class StudentServiceImpl implements StudentService {@Override
public int saveStudent(Student student) {return studentMapper.insert(student);
}
}
改用save方法
首先在实体类设置自增主键、然后extends
=======MAPPER=======
@Mapper
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}====Entity=========
public class Student {@TableId(type = IdType.AUTO)private Integer id;private String name;private Integer age;}
=======Service=======
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {@Override
public int saveStudent(Student student) {boolean res = save(student);if(!res){log.error("插入失败");//此时getId就能拿到主键了Integer sid = student.getId();return student;
}
}