ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题
用spring+ibatis写了一个登陆页面输入表名 然后在页面查询出表的数据,
sql配置文件
<![CDATA[
select * from (select rownum id,t.* from
$tableName$ t where rownum<= #endNum# ) b where b.id>= #startNum#
]]>
第一次运行服务器输入一个表名可以查询出表的所有信息,退出返回到登陆页面重新输入一个表名查询就会出错
日志显示:
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"也不行
请问我要怎样修改才能在重新输入表名后查询后返回新的表的结果了,请大家帮帮忙,谢谢
[b]问题补充:[/b]
感谢lggege的关注,目前只能重新启动才能重新查询另一张表的数据
[code="java"]
<![CDATA[
select * from (select rownum id,t.* from
$tableName$ t where rownum<= #endNum# ) b where b.id>= #startNum#
]]>
[/code]
这是我的测试代码:
[code="xml"]
<![CDATA[
SELECT * FROM $tableName$
]]>
[/code]
[code="java"] public Map getTableData(String tableName) {
return this.getSqlMapClientTemplate().queryForMap("getTableData", tableName, "tableName");
}[/code]
[code="java"] public void testGetTableData() {
Map values1 = this.articleDao.getTableData("one");
assertNotNull(values1);
Map<String, Object> values2 = this.articleDao.getTableData("two");
assertNotNull(values2);
}[/code]
运行期间的SQL:
[code="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]>[/code]
数据库表结构:
[code="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)[/code]
所以, 我的测试, 不会发生第二次查询结果Map 会是前一次查询的.
可能是因为 我的表one和表two 的schema是一样的, 我改下再测试.
[code="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)[/code]
[code="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.[/code]
确实发生了这个问题, 这是在第二个查询时抛出的异常..