通常来说只有一个参数且参数为对象时,只需要在sql语句中写属性名即可:
@Insert("insert into student(name, sex) values(#{name}, #{sex})")
int addStudent(Student student);
若有两个以上的基础类型参数时,需要加入@param
来指定参数名:
@Select("select * from student where sid = #{sid} and sex = #{sex}")
Student getStudentBySidAndSex(@Param("sid") int sid, @Param("sex") String sex);
若一个是基本类型一个是对象类型,仅仅为基本类型加@Param
而对象属性是直接添加属性名时也不行,需要修改为:
@Insert("insert into student(sid, name, sex) values(#{sid}, #{student.name}, #{student.sex})")
int addStudent(@Param("sid") int sid, @Param("student") Student student);
两个都添加@Param
注解并使用对象+属性名的方式访问。