您的位置:首页 > 其它

ibatis基础(七):模糊查询实体对象

2011-12-04 17:09 337 查看
在Student.xml中添加:

<select id="selectStudentByName" parameterClass="String" resultClass="Student">
select *
from student
where name like '%$name$%'
</select>


此处标红特别注意,跟以往写法不同,可以理解为##自动帮你添加引号,而$$不添加引号

进行单元测试:

@Test
public void queryStudentByName() throws Exception{
Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");//读取总配置文件
SqlMapClient sqlmapclient=SqlMapClientBuilder.buildSqlMapClient(reader);//创建SqlMapClient来操作数据库
reader.close();
List<Student> students=sqlmapclient.queryForList("selectStudentByName","i");//调用我们写在xml中的sql语句
for(Student student:students){
System.out.println(student);
}
}


查看打印结果,发现名字中带有"i"的信息都被查询出来了:

1    Billy    70.5
4    brian    100.0



ibatis的优点(与JDBC相比):

减少了61%的代码量(此处有吹牛的嫌疑)

简单

架构级性能增强

sql语句与代码分离

简化项目中的分工

增强了移植性

缺点:

sql需要自己写

参数只能有一个
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: