ibatis禁用缓存

来源:互联网 发布:php javascript 知乎 编辑:程序博客网 时间:2024/05/22 06:51

在最近的项目开发中遇到一个问题,前端传一个表名到后台。ibatis发起查询,ibatis的xml如下


<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"><sqlMap namespace="STORED_PROCEDURE"><!-- 根据前端传入的不同的表名查询结果集 --><select id="queryResultByTableName" parameterClass="Map" resultClass="HashMap">select * from $tableName$ </select></sqlMap>

 

测试的过程中发现第一次传入的表名是T_CITY没有问题,日志如下

2015-01-29 13:53:41 [com.fullgold.dbdst.action.QueryTableResultByTableName] INFO - process QueryTableResultByTableName invoke...
2015-01-29 13:53:41 [java.sql.Connection] DEBUG - {conn-100000} Connection
2015-01-29 13:53:41 [java.sql.Connection] DEBUG - {conn-100000} Preparing Statement:    select * from T_CITY  
2015-01-29 13:53:41 [java.sql.PreparedStatement] DEBUG - {pstm-100001} Executing Statement:    select * from T_CITY  
2015-01-29 13:53:41 [java.sql.PreparedStatement] DEBUG - {pstm-100001} Parameters: []
2015-01-29 13:53:41 [java.sql.PreparedStatement] DEBUG - {pstm-100001} Types: []
2015-01-29 13:53:41 [com.fullgold.dbdst.action.QueryTableResultByTableName] INFO - CITYID
2015-01-29 13:53:41 [com.fullgold.dbdst.action.QueryTableResultByTableName] INFO - PROID
2015-01-29 13:53:41 [com.fullgold.dbdst.action.QueryTableResultByTableName] INFO - CITYNAME

 

第二次传入的表名是T_PROVINCE报错,报错信息如下

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   --- The error occurred while applying a result map.  --- Check the queryResultByTableName-AutoResultMap.  --- Check the result mapping for the 'CITYID' property.  --- Cause: java.sql.SQLException: 列名无效org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)javax.servlet.http.HttpServlet.service(HttpServlet.java:617)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

报错信息中描述的列名无效,仔细查看指的是CITYID这列无效,这个列名是第一次的查询的表T_CITY的列到T_PROVINCE中去查当然查询不到了。

其实这个是ibatis的缓存引起的,解决方法需要我们手动关闭ibatis的缓存。关闭方法如下,在要执行的sql上加一句,如下面红色部分

<select id="queryResultByTableName" parameterClass="Map" resultClass="HashMap" remapResults="true" >
    select * from $tableName$
</select>

0 0
原创粉丝点击