`

ibatis resultClass==hashmap resultClass提供几种类型

 
阅读更多

今天遇到一个问题:

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/inspur/paas/attachment/dao/attachment.xml.  
--- The error occurred while applying a result map.  
--- Check the Attachment.getAppAttachmentByAppId-AutoResultMap.  
--- Check the result mapping for the 'id' property.  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: java.util.Map

 

 

 

<select id="getAppAttachmentByAppId" parameterClass="string" resultClass="hashmap">
		select app_id,email,attachment_id,id from gov_item_express_info  where app_id = #app_id#
	</select>

 

 

 resultClass="hashmap"  ---使用map报错

 

 

ibatis结果集resultClass的几种类型

http://www.jcodecraeer.com/a/chengxusheji/java/2013/0102/771.html

摘要 ibatis在编写sqlmap的查询时,可以配置多种输出格式,比如:实体类,hashmap,xml格式。 sqlmap中的hashmap和xml都是内置别名。 1.实体类: resultMap id="UserResult" class="User"result property="id" column="T_ID"/result property="name" column="T_NA

ibatis在编写sqlmap的查询时,可以配置多种输出格式,比如:实体类,hashmap,xml格式。
sqlmap中的hashmap和xml都是内置别名。

1.实体类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<resultMap id="UserResult" class="User">
  
<result property="id" column="T_ID"/>
  
<result property="name" column="T_NAME"/>
  
<result property="sex" column="T_SEX"/>
  
<result property="address" column="T_ADDRESS"/>
  
</resultMap>
  
   
  
<select id="selectAllUser" resultMap="UserResult"> select * from t_user </select>
  
  
List list = userdao.selectAllUser(); for(int i=0;i<list.size();i++) { System.out.println(list.get(i)); }

注:当作一个对象使用。

2.hashmap:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<select id="selectAllUser" resultClass="hashmap"> select * from t_user </select>
  
  
List list = userdao.selectAllUser();
  
for(int i=0;i<list.size();i++) {
  
Map map = (Map)list.get(i);
  
System.out.print(map.get("id")+" ");
  
System.out.print(map.get("name")+" ");
  
System.out.print(map.get("sex")+" ");
  
System.out.print(map.get("address"));
  
System.out.println();
  
}

注:当作一个键值对的MAP使用。

3.XML:

1
2
3
4
5
6
7
8
<select id="selectXML" resultClass="xml" xmlResultName="log"> select * from t_user </select>
List list = userdao.selectAllUser();
  
for(int i=0;i<list.size();i++) {
  
System.out.println(list.get(i));
  
}

 


XML结果:

1
2
3
4
<?xml version="1.0" encoding="UTF-8"?><log><id>1</id><name>hua</name><***>1</***><address>1</address></log>
<?xml version="1.0" encoding="UTF-8"?><log><id>2</id><name> zhupan </name><***>2</***><address>1</address></log>
<?xml version="1.0" encoding="UTF-8"?><log><id>4</id><name> 4 </name><***>4</***><address>1</address></log>
<?xml version="1.0" encoding="UTF-8"?><log><id>5</id><name> 5 </name><***>5</***><address>2</address></log>

 

分享到:
评论

相关推荐

    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缓存动态字段问题

    resultClass="java.util.HashMap" remapResults="true"&gt; select $fieldnames$ from $resourcetable$ where 1=1 &lt;include refid="select_data_by_condition"/&gt; ``` 这里的 `&lt;select&gt;` 标签定义了一个查询语句,...

    ibatis笔记

    - `&lt;select id="selectHashmap" parameterClass="int" resultClass="hashmap"&gt;`:定义查询操作。 - `select * from sexs, aawhere s.sid = a.aid and s.sid = #sid#`:联表查询。 - `&lt;select id="selectAADyna" ...

    ibatis的多参数查询.doc

    本文将详细介绍如何在ibatis中实现多参数查询,并探讨几种常见的解决方案。 #### 二、ibatis简介 ibatis(现更名为MyBatis)是一个优秀的持久层框架,它消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索...

    ibatis16个常用sql语句

    &lt;select id="com.fashionfree.stat.accesslog.model.StatMemberAction.selectActionIdsOfModule" resultClass="hashMap"&gt; select moduleId, actionId from StatMemberAction &lt;dynamic prepend="where"&gt; moduleId...

    ibatis环境搭建

    &lt;select id="selectAllEmp" resultClass="hashmap"&gt; SELECT * FROM EMP &lt;!-- 按工号查询员工 --&gt; &lt;select id="selectEmpByNo" resultClass="com.microserver.pojo.Emp" parameterClass="string"&gt; SELECT * ...

    Ibatis常用sql语句

    &lt;select id="com.fashionfree.stat.accesslog.model.StatMemberAction.selectActionIdsOfModule" resultClass="hashMap"&gt; select moduleId, actionId from StatMemberAction &lt;dynamic prepend="where moduleId in"&gt;...

    ibatis 开发指南

    - **事务管理**:ibatis支持两种类型的事务管理机制,分别是基于JDBC的事务管理和基于JTA的事务管理。 - **基于JDBC的事务管理机制**:这是最简单的事务管理方式,适合于单数据源的情况。 - **基于JTA的事务管理...

    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,并将参数放入...

    Spring and iBATIS

    &lt;select id="selectUser" parameterClass="java.lang.String" resultClass="User"&gt; SELECT * FROM USERS WHERE NAME = #value# ``` **步骤3:编写DAO接口和实现** 定义一个`UserDao`接口,并使用Spring的`...

Global site tag (gtag.js) - Google Analytics