`
xulongfa
  • 浏览: 70924 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ibatis Tips 之 java.util.Map作为parameterClass和resultClass

阅读更多

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

分享到:
评论

相关推荐

    ibatis 用HashMap解决resultClass映射

    &lt;select id="getDynamicTable" resultClass="java.util.HashMap" remapResults="true" parameterClass="java.lang.Integer"&gt; select t.* from some_table t where t.status = #{status} ``` 这里需要注意的是,`#...

    ibatis的错误总结

    然而,如果`roleId`是作为`Map`的一个键值对传递的,那么`parameterClass`应该被设置为`java.util.Map`。因此,正确的写法应该是: ```xml &lt;select id="queryIfSysNotificationPri" parameterClass="java.util.Map...

    iBatis-设置缓存模式-Java源码(下载)

    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; ...

    ibatis的多参数查询.doc

    &lt;select id="checkLogin2" parameterClass="java.util.Map" resultClass="java.lang.Integer"&gt; SELECT count(*) AS value FROM userinfo WHERE uid=#uid# and pwd=#pwd# ``` **Java代码:** ```java Map, ...

    ibatis list

    `&lt;select id="selectByIterate" parameterClass="java.util.List" resultClass="user"&gt;`示例中,通过`," open="(" close=")"&gt;#ids[]#&lt;/iterate&gt;`实现了基于`ids`列表的批量查询操作。这允许根据一组ID值检索`USERS`...

    ibatis2.0+sqlserver2005环境搭建

    &lt;select id="getAllUserList" parameterClass="java.util.Map" resultClass="User"&gt; SELECT * FROM test_user with (NOLOCK) ``` 这里定义了两个SQL查询,`getAllUsers`用于获取所有用户,`getAllUserList`则...

    Ibatis资料ibatai sql map iBATIS使用$和#的一些理解

    &lt;select id="getPeopleList" resultClass="model.User" parameterClass="java.util.Map"&gt; &lt;![CDATA[ select * from test where name like '%$name$%' ]]&gt; ``` 在Java代码中,我们创建一个HashMap,并将参数放入...

    解决IBatis缓存动态字段问题

    resultClass="java.util.HashMap" remapResults="true"&gt; select $fieldnames$ from $resourcetable$ where 1=1 ``` 这里的 `&lt;select&gt;` 标签定义了一个查询语句,其参数类型为 `HashMap`,返回结果也是 `...

    Ibatis多表查询

    &lt;select id="getUsersByBookId" parameterClass="int" resultClass="User"&gt; &lt;![CDATA[select id, book_oid, u.name from user u where book_oid = #value#]]&gt; ``` 在此配置文件中,我们定义了两个主要的操作:...

    ibatis_sqlMap的配置总结

    &lt;select id="findCustomers" parameterType="java.util.Map" resultMap="result_base"&gt; select * from customer where id=#value# and name=#name# ``` #### 六、结果集映射 iBatis的结果集映射机制非常强大,它...

    ibatis高级特性

    &lt;select id="getUsersByCriteria" parameterClass="java.util.Map" resultMap="get-user-result"&gt; &lt;![CDATA[ select id, name, sex from t_user != null"&gt; where name like #{name} ]]&gt; ``` 通过 `&lt;if&gt;`...

    Spring and iBATIS

    &lt;update id="executeProcedure" parameterClass="java.util.Map"&gt; {call my_procedure(#{param1}, #{param2})} ``` 在Java代码中,可以通过传递参数调用这个存储过程: ```java Map, Object&gt; params = new ...

Global site tag (gtag.js) - Google Analytics