ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题

来源:互联网 发布:apache 官网下载64位 编辑:程序博客网 时间:2024/04/30 04:23

ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题

标签: ibatisnulllistiteratorobjecthashmap
 10407人阅读 评论(0) 收藏 举报
 分类:

目录(?)[+]

 

ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题

悬赏:5 发布时间:2008-07-29 提问人:樊宝最帅 (初级程序员)

用spring+ibatis写了一个登陆页面输入表名 然后在页面查询出表的数据, 

sql配置文件 
</select> 
<select id="getTableDataByPage" resultClass="java.util.HashMap" parameterClass="java.util.HashMap"> 
<![CDATA[ 
select * from (select rownum id,t.* from 
$tableName$ t where rownum<= #endNum# ) b where b.id>= #startNum# 
]]> 
</select> 

第一次运行服务器输入一个表名可以查询出表的所有信息,退出返回到登陆页面重新输入一个表名查询就会出错 
日志显示: 
Cause: java.sql.SQLException: 列名无效 
com.ibatis.common.jdbc.exception.NestedSQLException: 
--- The error occurred in com/zf/querytest/bo/impl/tableDao.xml. 
--- The error occurred while applying a result map. 
--- Check the tableDao.getTableDataByPage-AutoResultMap. 
--- Check the result mapping for the 'TASKID' property. 

其中TASKID为上一张表中的字段,也就是ResultMap中保留了上个表的字段信息,将sqlMapClient中的cacheModelsEnabled设置为"false"也不行 
请问我要怎样修改才能在重新输入表名后查询后返回新的表的结果了,请大家帮帮忙,谢谢 
问题补充:
感谢lggege的关注,目前只能重新启动才能重新查询另一张表的数据

采纳的答案

2008-08-05 hetylei (初级程序员)


Java代码 
  1. </select>   
  2. <select id="getTableDataByPage" resultClass="java.util.HashMap" parameterClass="java.util.HashMap" remapResults="true">   
  3. <![CDATA[   
  4. select * from (select rownum id,t.* from   
  5. $tableName$ t where rownum<= #endNum# ) b where b.id>= #startNum#   
  6. ]]>   
  7. </select>   

提问者对于答案的评价:
好的 经测试解决了,原来有remapResults这个属性,谢谢你的帮助

其他回答

这是我的测试代码: 
Xml代码 
  1. <select id="getTableData" resultClass="java.util.HashMap" parameterClass="java.lang.String">   
  2.     <![CDATA[  
  3.     SELECT * FROM $tableName$ 
  4.     ]]>   
  5.    </select>   


Java代码 
  1. public Map<String, Object> getTableData(String tableName) {  
  2.     return this.getSqlMapClientTemplate().queryForMap("getTableData", tableName, "tableName");  
  3. }  


Java代码 
  1. public void testGetTableData() {  
  2.     Map<String, Object> values1 = this.articleDao.getTableData("one");  
  3.     assertNotNull(values1);  
  4.       
  5.     Map<String, Object> values2 = this.articleDao.getTableData("two");  
  6.     assertNotNull(values2);  
  7. }  


运行期间的SQL: 
Java代码 
  1. Executing Statement:         SELECT * FROM one  
  2. Parameters: []>  
  3. Types: []>  
  4. ResultSet>  
  5. Header: [id, name]>  
  6. Result: [1, z]>  
  7.   
  8. Executing Statement:         SELECT * FROM two         >  
  9. ...  
  10. Header: [id, name]>  
  11. Result: [2, zz]>  


数据库表结构: 
Sql代码 
  1. mysql> desc one;  
  2. +-------+-------------+------+-----+---------+-------+  
  3. | Field | Type        | Null | Key | Default | Extra |  
  4. +-------+-------------+------+-----+---------+-------+  
  5. | id    | int(20)     | YES  |     | NULL    |       |  
  6. name  | varchar(20) | YES  |     | NULL    |       |  
  7. +-------+-------------+------+-----+---------+-------+  
  8. rows in set (0.02 sec)  
  9.   
  10. mysql> desc two;  
  11. +-------+-------------+------+-----+---------+-------+  
  12. | Field | Type        | Null | Key | Default | Extra |  
  13. +-------+-------------+------+-----+---------+-------+  
  14. | id    | int(20)     | YES  |     | NULL    |       |  
  15. name  | varchar(20) | YES  |     | NULL    |       |  
  16. +-------+-------------+------+-----+---------+-------+  
  17. rows in set (0.00 sec)  
lggege (架构师) 2008-07-30 举报作弊
所以, 我的测试, 不会发生第二次查询结果Map 会是前一次查询的.
lggege (架构师) 2008-07-30 举报作弊
可能是因为 我的表one和表two 的schema是一样的, 我改下再测试.
lggege (架构师) 2008-07-30 举报作弊
Sql代码 
  1. mysql> desc one;  
  2. +---------+-------------+------+-----+---------+-------+  
  3. | Field   | Type        | Null | Key | Default | Extra |  
  4. +---------+-------------+------+-----+---------+-------+  
  5. | id      | int(20)     | YES  |     | NULL    |       |  
  6. name    | varchar(20) | YES  |     | NULL    |       |  
  7. | remarks | varchar(20) | YES  |     | NULL    |       |  
  8. +---------+-------------+------+-----+---------+-------+  
  9. rows in set (0.00 sec)  
  10.   
  11. mysql> desc two;  
  12. +-------+-------------+------+-----+---------+-------+  
  13. | Field | Type        | Null | Key | Default | Extra |  
  14. +-------+-------------+------+-----+---------+-------+  
  15. | id    | int(20)     | YES  |     | NULL    |       |  
  16. name  | varchar(20) | YES  |     | NULL    |       |  
  17. +-------+-------------+------+-----+---------+-------+  
  18. rows in set (0.01 sec)  



Java代码 
  1. org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:     
  2. --- The error occurred in cn/iwoo/demo/dao/maps/Article.xml.    
  3. --- The error occurred while applying a result map.    
  4. --- Check the getTableData-AutoResultMap.    
  5. --- Check the result mapping for the 'remarks' property.    
  6. --- Cause: java.sql.SQLException: Column 'remarks' not found.  

确实发生了这个问题, 这是在第二个查询时抛出的异常..

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

1.Map作为parameterClass

映射文件:

[xml] view plain copy
 print?
  1. <!--use Map type as parameterClass-->  
  2.         <select id="getProduct-Map" parameterClass="java.util.Map" resultMap="get-product-result">  
  3.             <![CDATA[ 
  4.                 select * from t_product 
  5.                 where prd_id=#id# and prd_description=#description# 
  6.             ]]>  
  7.         </select>  

?DAO层:

[java] view plain copy
 print?
  1. /** 
  2.      * java.util.Map作为parameterClass 
  3.      */  
  4.     public Product getProductMap(Map map) throws SQLException {  
  5.         init();  
  6.         Product product = (Product)sqlMapClient.queryForObject("getProduct-Map", map);  
  7.         return product;  
  8.     }  

?Test类:

[java] view plain copy
 print?
  1. public void getProductMap() throws SQLException{  
  2.         Map map = new HashMap();  
  3.         map.put("id"new Integer(1));  
  4.         map.put("description""basketball");  
  5.         Product product = productDao.getProductMap(map);  
  6.         System.out.println(product);  
  7.     }  

?结果:

id:1
description:basketball
price206.99

?

?

2.Map作为resultClass

映射文件:

[xml] view plain copy
 print?
  1. <resultMap id="get-product-map" class="<a name="baidusnap1"></a><strong>java.util.HashMap</strong>">  
  2.             <result property="id" column="prd_id"/>  
  3.             <result property="description" column="prd_description"/>  
  4.             <result property="price" column="prd_price"/>  
  5.         </resultMap>  
  6.   
  7. <!--START  use Map type as <strong>resultClass</strong>,MUST use <strong>java.util.HashMap</strong> instead java.util.Map-->  
  8.         <select id="getProdcut-MapResult" <strong>resultClass</strong>="<span><strong>java.util.HashMap</strong></span>  
  9. ">  
  10.             <![CDATA[ 
  11.                 select * from t_product 
  12.             ]]>  
  13.         </select>  
  14.           
  15.         <select id="getProductUseMap-resultMap" resultMap="get-product-map">  
  16.             <![CDATA[ 
  17.                 select * from t_product 
  18.             ]]>  
  19.         </select>  
  20.         <!-- END  -->  

?DAO层:

[java] view plain copy
 print?
  1. /** 
  2.      * java.util.Map作为<strong>resultClass</strong> 
  3.      */  
  4.     public List getProductMapResult() throws SQLException {  
  5.         init();  
  6.         List list = sqlMapClient.queryForList("getProdcut-MapResult");  
  7.         return list;  
  8.           
  9.     }  
  10.   
  11.   
  12.     public List getProductUseMapByResultMap() throws SQLException {  
  13.         init();  
  14.         List list = sqlMapClient.queryForList("getProductUseMap-resultMap");  
  15.         return list;  
  16.     }  

?Test类:

[java] view plain copy
 print?
  1. public void getProductMapResult() throws SQLException{  
  2.         Map map = null;  
  3.         List list = productDao.getProductMapResult();  
  4.         for(Iterator it=list.iterator(); it.hasNext();) {  
  5.             //List里存放的是java.util.Map类型  
  6.             Object obj = (Object)it.next();  
  7.             System.out.println(obj.getClass());  
  8.             System.out.println(obj);  
  9.         }  
  10.     }  
  11.       
  12.     public void getProductUseMapByResultMap() throws SQLException {  
  13.         Map map = null;  
  14.         List list = productDao.getProductUseMapByResultMap();  
  15.         for(Iterator it=list.iterator(); it.hasNext();) {  
  16.             //List里存放的是java.util.Map类型  
  17.             Object obj = (Object)it.next();  
  18.             System.out.println(obj.getClass());  
  19.             System.out.println(obj);  
  20.         }  
  21.     }  
?

结果:

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

0 0
原创粉丝点击