MyBatis 註解版(五)Spring boot 註解系列 插入物件返回 id

沒事偷著樂琅發表於2017-11-07
public interface StudentMapper {
    @Insert("insert into student(name,credit_hour) value(#{name},#{creditHour})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//加入該註解可以保持物件後,檢視物件插入id
    public int insert(Student s);
    }
@Service
public class StudentService {

    @Resource
    private StudentMapper mStudentMapper;

    public int insert(Student s){
        mStudentMapper.insert(s);
        return s.getId();
    }
}

相關文章