Mybatis使用註解進行增刪改查
阿新 • • 發佈:2018-11-22
// 增
public interface StudentMapper{ @Insert("insert into student (stud_id, name, email, addr_id, phone)values(#{studId},#{name},#{email},#{address.addrId},#{phone})") int insertStudent(Student student); } public interface StudentMapper{ @Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})") @Options(useGeneratedKeys=true,keyProperty="studId") int insertStudent(Student student); } public interface StudentMapper{ @Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})") @SelectKey(statement="select stud_id_seq.nextval from dual",keyProperty="studId",resultType=int.calss,before=true) int insertStudent(Student student); }
// 改 @Update("update students set name=#{name},email=#{email}") int updateStudent(Student student);
// 刪 @Delete("delete form students where stud_id=#{studId}") int deleteStudent(int studId)
// 查 @Select("select name,email,phone from students where stud_id=#{studId}") Student findStudentById(Integer studId);
推薦使用xml檔案進行配置,方面後續修改容易