[转]iBATIS查询条件为多个参数
2016-09-20 19:22
316 查看
原文:http://hi.baidu.com/wtx521/item/1ac70d0f1f518e8302ce1bad
当查询记录的时候,过滤记录的条件为多个的时候你就不能使用 parameterClass=int 等了,这个参数只能使用一次。
有两种解决方案:
1.使用对象构造查询参数
步骤:
a.改写student.xml
b.写实现
Java代码
c.测试
Java代码
d.结果
172 [main] INFO cn.com.xinli.ibatis.dao.impl.StudentDAOImpl(160) - score: 2009-06-17
2.使用map封装查询参数
a.改写student.xml,map 的定义一定要在sql的前面,第一次我做连写的时候就吧map的定义放在了后面,结果老是报错,说找不到map的定义,还就得就是 占位符一定要使用 ? 号
b.实现
Java代码
c.测试
Java代码
d.结果
172 [main] INFO cn.com.xinli.ibatis.dao.impl.StudentDAOImpl(160) - score: 2009-06-17
当查询记录的时候,过滤记录的条件为多个的时候你就不能使用 parameterClass=int 等了,这个参数只能使用一次。
有两种解决方案:
1.使用对象构造查询参数
步骤:
a.改写student.xml
<select id="selectStudentByIdAndName" resultClass="Student" parameterClass="Student"> select * from student where sid=#sid# and sname=#sname# </select>
b.写实现
Java代码
public Student queryStudentsByNameAndId(Student student) { Student s=null; try { s=(Student)sqlMapClient.queryForObject("selectStudentByIdAndName",student); } catch (SQLException e) { e.printStackTrace(); } return s; }
c.测试
Java代码
StudentDAO studentDAO=new StudentDAOImpl(); Student student=new Student(); student.setSid(326); student.setSname("胡晓亮3"); Student s=studentDAO.queryStudentsByNameAndId(student); log.info("score: "+s.getBirth());
d.结果
172 [main] INFO cn.com.xinli.ibatis.dao.impl.StudentDAOImpl(160) - score: 2009-06-17
2.使用map封装查询参数
a.改写student.xml,map 的定义一定要在sql的前面,第一次我做连写的时候就吧map的定义放在了后面,结果老是报错,说找不到map的定义,还就得就是 占位符一定要使用 ? 号
<parameterMap class="java.util.HashMap" id="parameterMap"> <parameter property="sid"/> <parameter property="sname"/> </parameterMap> <select id="selectStudentByIdAndName" resultClass="Student" parameterMap="parameterMap"> select * from student where sid=? and sname=? </select>
b.实现
Java代码
public Student queryStudentsByNameAndId(HashMap<String,String> hashMap) { Student s=null; try { s=(Student)sqlMapClient.queryForObject("selectStudentByIdAndName",hashMap); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
c.测试
Java代码
StudentDAO studentDAO=new StudentDAOImpl(); HashMap m=new HashMap(); m.put("sname", "小虎"); m.put("sid", 12434); Student s=studentDAO.queryStudentsByNameAndId(m); log.info("score: "+s.getBirth());
d.结果
172 [main] INFO cn.com.xinli.ibatis.dao.impl.StudentDAOImpl(160) - score: 2009-06-17
相关文章推荐
- ibatis简单实现与配置
- Mybatis与Ibatis的区别
- The error occurred while applying a result map.
- ibatis 的缓存
- ibatis 简单使用
- Mapped Statements collection does not contain
- 使用ibatis返回复合对象(一对多)
- ibatis 中传入list对象
- 简单实现ibatis的物理分页
- ibatis加memcached缓存
- ibatis 高速缓存
- 最近在用ibatis,发些文章来学习下
- 学习ibatis,参照夏晰翻译的ibatis2.0学习指南
- 从IBatis2.X 移植到IBatis3.0 sqlMapConfig and sqlMap XML 配置文件升级说明
- iBATIS教程之快速入门浅析
- struts2.1.8+mybatis3.0+spring3.0(全注解)小例子
- iBatis2--SqlMap的配置总结
- ibatis 动态Sql
- Hibernate和ibatis在同一个webapp中混合使用在spring中的配置方法
- How to use custom type handler (typeHandler) in ibatis 2.3.4