-
ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题5
用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年7月29日 16:58
5个答案 按时间排序 按投票排序
-
采纳的答案
</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>
2008年8月05日 13:56
-
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)
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.
确实发生了这个问题, 这是在第二个查询时抛出的异常..2008年7月30日 10:17
-
这是我的测试代码:
<select id="getTableData" resultClass="java.util.HashMap" parameterClass="java.lang.String"> <![CDATA[ SELECT * FROM $tableName$ ]]> </select>
public Map<String, Object> getTableData(String tableName) { return this.getSqlMapClientTemplate().queryForMap("getTableData", tableName, "tableName"); }
public void testGetTableData() { Map<String, Object> values1 = this.articleDao.getTableData("one"); assertNotNull(values1); Map<String, Object> values2 = this.articleDao.getTableData("two"); assertNotNull(values2); }
运行期间的SQL: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]>
数据库表结构: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)
2008年7月30日 10:10
相关推荐
ibatis 用HashMap解决Ibatis未知列名和列数的查询结果的resultClass映射
import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.icss.model.Employee...
import java.util.List; import com.ibatis.sqlmap.client.SqlMapClient; public class Main { public static void main(String[] a) throws Exception { Util util = new Util(); util ....
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 <bean id="sqlMapClient" class="org.spring...
ibatis中使用安全的拼接语句,动态查询,大于、小于、等于.doc
#log4j.logger.java.sql.Connection = INFO #log4j.logger.java.sql.Statement = DEBUG #log4j.logger.java.sql.PreparedStatement = DEBUG #log4j.logger.java.sql.ResultSet = DEBUG #2 定義A1輸出到控制器 ...
ibatis_动态查询条件详解及需要注意的地方
ibatis-2.3.4.726.jar,ibatis-2.3.0.677.jar,ibatis-2.3.3.720.jar下载,都已放入一个压缩包下。
关于实体映射文件中namespace,以前的版本是可选的内容,现在被用来与接口绑定,也就是说把接口的实现转移到xml文件中来了,以后的维护会相当的方便。 第一篇的根据id查询学生的例子中,稍做修改: 建立StudentDAO...
Ibatis多表查询
这是EMIS文件中缺少的一个ibatis工具包,因上传的时候出错没有放上去。。。。请大家原谅
<resultMap class="java.util.HashMap" id="zjxxs"> <result property="zcxx01" column="ZCXX01"/> <result property="activityid" column="ACTIVITYID"/> <result property="zcxx37" column="ZCXX37"/> ...
使用的ognl,正对是struts2 hibernate 等
Ibatis多表查询,一个小小的多表查询实例教你如何用ibatis进行多表查询
ibatis多表查询开发过程(java)
import java.util.Map; public class SimplePooledConnection implements InvocationHandler { // Fields private static final String CLOSE = "close"; private static final Class[] IFACES; private int ...
Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("com/itcast/SqlMapConfig.xml"); sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader); ...
import java.util.List; public interface UserService { //查询全部 public List<User> selectAll(); } package com.example.supplier.dao; import com.example.supplier.entity.SysLogInfo; import org....
查询出列表,也就是返回list, 在我们这个例子中也就是 List<User> , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...