ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题
2016-09-27 21:16
549 查看
ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题
标签: ibatisnulllistiteratorobjecthashmap2009-10-29 12:59 10407人阅读 评论(0) 收藏 举报
分类:
我自己看的(50)
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
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代码</select>
<select id="getTableDataByPage" resultClass="java.util.HashMap" parameterClass="java.util.HashMap" remapResults="true">
<![CDATA[
select * from (select rownum id,t.* from
$tableName$ t where rownum<= #endNum# ) b where b.id>= #startNum#
]]>
</select>
提问者对于答案的评价:
好的 经测试解决了,原来有remapResults这个属性,谢谢你的帮助
其他回答
这是我的测试代码:Xml代码
<select id="getTableData" resultClass="java.util.HashMap" parameterClass="java.lang.String">
<![CDATA[
SELECT * FROM $tableName$
]]>
</select>
Java代码
public Map<String, Object> getTableData(String tableName) {
return this.getSqlMapClientTemplate().queryForMap("getTableData", tableName, "tableName");
}
Java代码
public void testGetTableData() {
Map<String, Object> values1 = this.articleDao.getTableData("one");
assertNotNull(values1);
Map<String, Object> values2 = this.articleDao.getTableData("two");
assertNotNull(values2);
}
运行期间的SQL:
Java代码
Executing Statement: SELECT * FROM one
Parameters: []>
Types: []>
ResultSet>
Header: [id, name]>
Result: [1, z]>
Executing Statement: SELECT * FROM two >
...
Header: [id, name]>
Result: [2, zz]>
数据库表结构:
Sql代码
mysql> desc one;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> desc two;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
lggege (架构师) 2008-07-30 举报作弊
所以, 我的测试, 不会发生第二次查询结果Map 会是前一次查询的.
lggege (架构师) 2008-07-30 举报作弊
可能是因为 我的表one和表two 的schema是一样的, 我改下再测试.
lggege (架构师) 2008-07-30 举报作弊
Sql代码
mysql> desc one;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| remarks | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> desc two;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
Java代码
org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in cn/iwoo/demo/dao/maps/Article.xml.
--- The error occurred while applying a result map.
--- Check the getTableData-AutoResultMap.
--- Check the result mapping for the 'remarks' property.
--- Cause: java.sql.SQLException: Column 'remarks' not found.
确实发生了这个问题, 这是在第二个查询时抛出的异常..
ibatis Tips
之 java.util.Map作为parameterClass和resultClass
1.Map作为parameterClass映射文件:
[xml] view
plain copy
print?
<!--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] view
plain copy
print?
/**
* java.util.Map作为parameterClass
*/
public Product getProductMap(Map map) throws SQLException {
init();
Product product = (Product)sqlMapClient.queryForObject("getProduct-Map", map);
return product;
}
?Test类:
[java] view
plain copy
print?
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
映射文件:
[xml] view
plain copy
print?
<resultMap id="get-product-map" class="<a name="baidusnap1"></a><strong>java.util.HashMap</strong>">
<result property="id" column="prd_id"/>
<result property="description" column="prd_description"/>
<result property="price" column="prd_price"/>
</resultMap>
<!--START use Map type as <strong>resultClass</strong>,MUST use <strong>java.util.HashMap</strong> instead java.util.Map-->
<select id="getProdcut-MapResult" <strong>resultClass</strong>="<span><strong>java.util.HashMap</strong></span>
">
<![CDATA[
select * from t_product
]]>
</select>
<select id="getProductUseMap-resultMap" resultMap="get-product-map">
<![CDATA[
select * from t_product
]]>
</select>
<!-- END -->
?DAO层:
[java] view
plain copy
print?
/**
* java.util.Map作为<strong>resultClass</strong>
*/
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类:
[java] view
plain copy
print?
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中动态查询表返回用resultClass="java.util.HashMap" 的问题
- ibatis中动态查询返回字段返回用resultClass="java.util.HashMap" Java.sql.SQLException: 列名无效的问题
- ibatis中动态查询返回字段返回用resultClass="java.util.HashMap" 的问题
- ibatis HashMap select insert update delete parameterMap parameterClass resultMap resultClass="java.util.HashMap" 不写实体类
- ibatis resultclass "java.util.hashmap" 缓存
- ibatis中parameterClass="java.util.HashMap",使用多个参数
- ibatis中配置parameterClass="java.util.HashMap"的用法
- ibatis中配置parameterClass="java.util.HashMap"的用法
- ibatis中配置parameterClass="java.util.HashMap"的用法
- ibatis用resultClass动态返回查询结果
- ibatis 返回list<String[]>----resultClass="hashMap" 可以实现
- iBaits中,关于insert返回值的问题(注意!!!不必写resultClass= "java.lang.Integer",方法的返回值就是int)
- org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.util.HashMap
- mybatis中的resultType="java.util.Map",返回有重复数据
- ibatis 用HashMap解决Ibatis未知列名和列数的查询结果的resultClass映射
- 【Ibatis】(十一)、java.util.Map作为parameterClass和resultClass
- springMVC返回json格式数据报错:No converter found for return value of type: class java.util.HashMap
- mybatis 之 parameterType="String" resultType="java.util.HashMap">
- ibatis.net查询float类型返回实体类或者ResultMap时无效的问题
- ibatis java.util.Map作为parameterClass和resultClass(转)