如下代码所示,dao接口中insertStudent方法没有使用@Param注解:
public interface StudentDao {
int insertStudent(Student student);
int delStudent(int id);
int updateStudent(@Param("student") Student student);
Student queryById(int id);
List<Student> queryAll(@Param("offset") int offset,@Param("limit") int limit);
}
mapper文件中insertStudent方法的实现,并在parameterType中指定了Student的具体类型:
<insert id="insertStudent" parameterType="org.student.entity.Student">
insert ignore into student (name,sex,grade) VALUES (#{student.name},#{student.sex},#{student.grade});
</insert>
junit测试代码:
@Test
public void testInsertStudent() throws Exception {
System.out.println(studentDao.insertStudent(new Student("李四",0,62)));
}
测试报错:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'student' in 'class org.student.entity.Student'
dao接口代码:
import org.student.entity.Student;
public interface StudentDao {
int insertStudent(@Param("student")Student student);
int delStudent(int id);
int updateStudent(@Param("student") Student student);
Student queryById(int id);
List<Student> queryAll(@Param("offset") int offset,@Param("limit") int limit);
}
mapper实现代码:
<mapper namespace="org.student.dao.StudentDao">
<insert id="insertStudent">
insert ignore into student (name,sex,grade) VALUES (#{student.name},#{student.sex},#{student.grade});
</insert>
<delete id="delStudent">
delete from student where id=#{id};
</delete>
<update id="updateStudent">
update student set name=#{student.name},sex=#{student.sex},grade=#{student.grade} where id=#{student.id};
</update>
<select id="queryById" resultType="Student">
select id,name,sex,grade from student where id=#{id};
</select>
<select id="queryAll" resultType="Student">
select id,name,sex,grade from student
order by id
limit #{offset},#{limit};
</select>
insertStudent 方法的student参数不加Param注解junit测试就会报
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'student' in 'class org.student.entity.Student'