1、实现IStudentDAOImpl类中的queryStudengById()函数,代码如下:
![]()
@SuppressWarnings("unchecked")
@Override
public Student queryStudengById(int id) {
@SuppressWarnings("unused")
List<Student> studentList = null;
Student student = new Student();
try {
//第一种方式
/*studentList = sqlMapClient.queryForList("selectStudentById", id);
student = studentList.get(0);*/
//第二种方式
student = (Student) sqlMapClient.queryForObject("selectStudentById", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}
2、Student.xml文件中添加如下的代码:
![]()
<select id="selectStudentById" parameterClass="int" resultClass="Student">select * from STUDENT s where s.sid=#sid#</select>
#sid# 相当如 sql绑定变量中的问号占位符。