错误信息:
org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in jndi:/localhost/studen/WEB-INF/app-config/query/sqlmap/Test.xml.--- The error occurred while applying a result map.
--- Check the QueryReport_QueryBucketTest.SELECT_QUERYUBUCKETTESTDETAIL_CN-AutoResultMap.
--- Check the result mapping for the 'LPV_CNT_1' property.
--- Cause: Java.sql.SQLException: 列名无效
SQLMAP:
<select id="getWarningRecordListForXXFK" resultClass="java.util.HashMap" parameterClass="java.util.HashMap"> SELECT * FROM $tableName$ WHERE MR_BatchNo IN ( <iterate property="requestList" conjunction=","> #requestList[].ApplyForID# </iterate> ) </select>
表名动态传参,获取的结果集也是以Map形式动态返回的
Map<String,Object> paramap = new HashMap<String, Object>();//查询的参数Map paramap.put("tableName",standardType.getScorrespondTable()); paramap.put("requestList",requestList);String sqlId=getEntityName()+".getWarningRecordListForJSHD";List lstResult=warningDao.getSqlMapClientTemplate().queryForList(sqlId, paramap);//查询出来的结果
通过这种方式去接收结果集for(int i=0;i<lstResult.size();i++) { Map result = (Map)lstResult.get(i); String cardNo=result.get("CardNo").toString();}
因为ibatis保留列名为第一次查询时候的列名,第二次查另外一张表的时候,这张表中并没有第一张表的 列,所以就会报列名不存在!!
ibatis可以设置一个参数remapResults="true",每次查询都重新更新列名。
感谢 seng3018 的分享,原文地址:http://blog.csdn.net/seng3018/article/details/7550186