ibatis基础(三):查询指定id的单个对象

在Student.xml中插入如下代码:

    <!-- parameterClass代表要传入的参数类型  -->
<select id="SelectStudentById" parameterClass="int" resultClass="Student">
select *
from student
where id=#id#
</select>

然后在junit中进行测试:

1     @Test
2 public void queryStudentById() throws Exception{//根据Id查学生
3 Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");//读取总配置文件
4 SqlMapClient sqlmapclient=SqlMapClientBuilder.buildSqlMapClient(reader);//创建SqlMapClient来操作数据库
5 reader.close();
6 Student student=(Student) sqlmapclient.queryForObject("SelectStudentById",4);//调用我们写在xml中的sql语句
7 System.out.println(student);
8 }

打印如下结果:

4    brian    100.0





posted @ 2011-12-04 15:27  一直在等  阅读(639)  评论(0编辑  收藏  举报