1.Map作为parameterClass
映射文件:
<!--use Map type as parameterClass-->
<select id="getProduct-Map" parameterClass="java.util.Map" resultMap="get-product-result">
<![CDATA[
select * from t_product
where prd_id=#id# and prd_description=#description#
]]>
</select>
DAO层:
/**
* java.util.Map作为parameterClass
*/
public Product getProductMap(Map map) throws SQLException {
init();
Product product = (Product)sqlMapClient.queryForObject("getProduct-Map", map);
return product;
}
Test类:
public void getProductMap() throws SQLException{
Map map = new HashMap();
map.put("id", new Integer(1));
map.put("description", "basketball");
Product product = productDao.getProductMap(map);
System.out.println(product);
}
结果:
id:1
description:basketball
price206.99
2.Map作为resultClass
映射文件:
<resultMap id="get-product-map" class="java.util.HashMap">
<result property="id" column="prd_id"/>
<result property="description" column="prd_description"/>
<result property="price" column="prd_price"/>
</resultMap>
<!--START use Map type as resultClass,MUST use java.util.HashMap instead java.util.Map-->
<select id="getProdcut-MapResult" resultClass="java.util.HashMap
">
<![CDATA[
select * from t_product
]]>
</select>
<select id="getProductUseMap-resultMap" resultMap="get-product-map">
<![CDATA[
select * from t_product
]]>
</select>
<!-- END -->
DAO层:
/**
* java.util.Map作为resultClass
*/
public List getProductMapResult() throws SQLException {
init();
List list = sqlMapClient.queryForList("getProdcut-MapResult");
return list;
}
public List getProductUseMapByResultMap() throws SQLException {
init();
List list = sqlMapClient.queryForList("getProductUseMap-resultMap");
return list;
}
Test类:
public void getProductMapResult() throws SQLException{
Map map = null;
List list = productDao.getProductMapResult();
for(Iterator it=list.iterator(); it.hasNext();) {
//List里存放的是java.util.Map类型
Object obj = (Object)it.next();
System.out.println(obj.getClass());
System.out.println(obj);
}
}
public void getProductUseMapByResultMap() throws SQLException {
Map map = null;
List list = productDao.getProductUseMapByResultMap();
for(Iterator it=list.iterator(); it.hasNext();) {
//List里存放的是java.util.Map类型
Object obj = (Object)it.next();
System.out.println(obj.getClass());
System.out.println(obj);
}
}
结果:
class java.util.HashMap
{prd_id=1, prd_price=206.99, prd_description=basketball}
class java.util.HashMap
{prd_id=2, prd_price=106.99, prd_description=football}
class java.util.HashMap
{price=206.99, description=basketball, id=1}
class java.util.HashMap
{price=106.99, description=football, id=2}
注意:
Map作为resultClass时,必须指定具体的实现类,比如java.util.HashMap,否则会报错
Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class. Cause: java.lang.InstantiationException: java.util.Map
分享到:
相关推荐
<select id="getDynamicTable" resultClass="java.util.HashMap" remapResults="true" parameterClass="java.lang.Integer"> select t.* from some_table t where t.status = #{status} ``` 这里需要注意的是,`#...
然而,如果`roleId`是作为`Map`的一个键值对传递的,那么`parameterClass`应该被设置为`java.util.Map`。因此,正确的写法应该是: ```xml <select id="queryIfSysNotificationPri" parameterClass="java.util.Map...
File: Util.java import java.io.Reader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; ...
<select id="checkLogin2" parameterClass="java.util.Map" resultClass="java.lang.Integer"> SELECT count(*) AS value FROM userinfo WHERE uid=#uid# and pwd=#pwd# ``` **Java代码:** ```java Map, ...
`<select id="selectByIterate" parameterClass="java.util.List" resultClass="user">`示例中,通过`," open="(" close=")">#ids[]#</iterate>`实现了基于`ids`列表的批量查询操作。这允许根据一组ID值检索`USERS`...
<select id="getAllUserList" parameterClass="java.util.Map" resultClass="User"> SELECT * FROM test_user with (NOLOCK) ``` 这里定义了两个SQL查询,`getAllUsers`用于获取所有用户,`getAllUserList`则...
<select id="getPeopleList" resultClass="model.User" parameterClass="java.util.Map"> <![CDATA[ select * from test where name like '%$name$%' ]]> ``` 在Java代码中,我们创建一个HashMap,并将参数放入...
resultClass="java.util.HashMap" remapResults="true"> select $fieldnames$ from $resourcetable$ where 1=1 ``` 这里的 `<select>` 标签定义了一个查询语句,其参数类型为 `HashMap`,返回结果也是 `...
<select id="getUsersByBookId" parameterClass="int" resultClass="User"> <![CDATA[select id, book_oid, u.name from user u where book_oid = #value#]]> ``` 在此配置文件中,我们定义了两个主要的操作:...
<select id="findCustomers" parameterType="java.util.Map" resultMap="result_base"> select * from customer where id=#value# and name=#name# ``` #### 六、结果集映射 iBatis的结果集映射机制非常强大,它...
<select id="getUsersByCriteria" parameterClass="java.util.Map" resultMap="get-user-result"> <![CDATA[ select id, name, sex from t_user != null"> where name like #{name} ]]> ``` 通过 `<if>`...
<update id="executeProcedure" parameterClass="java.util.Map"> {call my_procedure(#{param1}, #{param2})} ``` 在Java代码中,可以通过传递参数调用这个存储过程: ```java Map, Object> params = new ...