parameterMap的应用
ibatis中的parameterMap的应用
在上篇文章中我们介绍了用ibatis实现了基本的增,删,改,查的功能,今天我们要介绍的是ibatis中的parameterMap的应用,我们结合具体的实例来讲解。(我们只是在上篇文章中的例题中新添一些内容,下面列出的是新添的内容)
1.映射配置(person.xml)
<!-- 测试parameterMap--> <parameterMap class="java.util.Map" id="query_some_param"> <parameter property="firstName" javaType="java.lang.String" jdbcType="VARCHAR"/> <parameter property="lastName" javaType="java.lang.String" jdbcType="VARCHAR"/> </parameterMap> <select id="querySome" resultClass="Person" parameterMap="query_some_param"> <![CDATA[ select * from person where firstName =? and lastName=? ]]> </select>
2.Dao(personDao.java)
public Map querySome(Map map, String key){
return getSqlMapClientTemplate().queryForMap("person.querySome", map, key);
}
3.Service
(1)Service接口(IPersonService)
public Map querySome(Map map, String key);
(2)Service实现类(PersonService)
public Map querySome(Map map, String key) {
return personDao.querySome(map, key);
}
4.Action(PersonAction)
public String querySome(){
System.out.println("Enter into.........");
Map map = new HashMap();
map.put("firstName", "英");
map.put("lastName", "黄");
Map result = getPersonService().querySome(map, "id");
for(Iterator it=result.keySet().iterator(); it.hasNext();){
Integer pId = (Integer)it.next();
Person pObject = (Person)result.get(pId);
System.out.println("************************************");
System.out.println("firstName"+pObject.getFirstName());
System.out.println("lastName"+pObject.getLastName());
System.out.println("birthDate"+pObject.getBirthDate());
System.out.println("weight"+pObject.getWeight());
System.out.println("height"+pObject.getHeight());
System.out.println("************************************");
}
System.out.println("result.size():"+result.size());
return null;
}
针对以上这个例题,我的数据库的数据如下:
id firstName lastName birthDate weight height
3 映容 江 1984-08-21 00:00:00 45 160
4 英 黄 1988-09-20 00:00:00 40 160
5 萧英 李 1988-09-13 00:00:00 60 168
6 英 黄 1985-08-20 14:49:20 90 179
7 英 黄 2009-09-03 14:50:16 4 50
输出的结果如下:
************************************
firstName英
lastName黄
birthDateTue Sep 20 00:00:00 CST 1988
weight40.0
height160.0
************************************
************************************
firstName英
lastName黄
birthDateTue Aug 20 14:49:20 CST 1985
weight90.0
height179.0
************************************
************************************
firstName英
lastName黄
birthDateThu Sep 03 14:50:16 CST 2009
weight4.0
height50.0
************************************
result.size():3
在我们写
<select id="querySome" resultClass="Person" parameterMap="query_some_param"> <![CDATA[ select * from person where firstName =? and lastName=? ]]> </select>
这段代码时,一定要写占位符"?",如果不用,而是写成如下这样
<select id="querySome" resultClass="Person" parameterMap="query_some_param"> <![CDATA[ select * from person where firstName like '%$firstName$%' and lastName like '%$lastName$%' ]]> </select>
那么,就会报如下的错误
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
关于Dao中调用的queryForMap方法中的参数key就是指定查出的属性中以那个属性的值作为返回的map中的键。就如上面的例题,result的键分别为4,6,7,值分别为id等于4,6,7的对象。