最近项目需要,做了一个动态查询。把查询结果返回到页面上显示。
要查询的字段和表名都是动态的,是在后台拼出来的。
可是在ibatis中运行的时候总报“列名无效”,在sql/plus中运行则正常。
百思不得其解。
后来通过查资料,解决如下:
<select id="queryLogInfo" resultClass="java.util.HashMap" remapResults= "true " parameterClass="java.util.Map" > select t.CREATOR,t.CREATE_DT,decode(o.BUSI_OPER_TYPE,'1','新增','2','修改','3','删除') as OPER_TYPE, $colNames$ from $tableName$ t,TAS_OPERATION_LOG o where $pkIdName$ = #pkIdValue# and t.log_id = #logId# </select>
其中:remapResults= "true"是必须的,否则会报列名无效的错误。
前台画表格:
function doSelectAction(Re){ //返回的串 var str = Re.responseText.evalJSON(); var keys = str.keys; var showNames = str.showNames; //表头显示名称 var showNamesArray = showNames.split(","); //sqlMap对应的字段名。 var keysArray = keys.split(","); //查询结果 var tem = str.logStrlist.evalJSON(); //先清空表格 document.getElementById("newbody").innerText = ''; //画表格头 var row=document.createElement("tr"); for(var j=0;j<showNamesArray.length;j++){ var cell = document.createElement("td"); cell.align = 'center'; cell.appendChild(document.createTextNode(showNamesArray[j])); row.appendChild (cell); } document.getElementById("newHead").appendChild (row); //画表格 tem.each(function(obj){ var row=document.createElement("tr"); for(var i=0;i<keysArray.length;i++){ var cell = document.createElement("td"); cell.align = 'center'; cell.appendChild(document.createTextNode(obj[keysArray[i]])); row.appendChild (cell); } document.getElementById("newbody").appendChild(row); }); //senfe("表格名称","奇数行背景","偶数行背景","鼠标经过背景","点击后背景"); senfe("newbody","#f8fbfc","#e5f1f4","#ecfbd4","#bce774"); }
function senfe(o,a,b,c,d){ var t=document.getElementById(o).getElementsByTagName("tr"); for(var i=0;i<t.length;i++){ t[i].style.backgroundColor=(t[i].sectionRowIndex%2==0)?a:b; // t[i].οnclick=function(){ // if(this.x!="1"){ // this.x="1"; // this.style.backgroundColor=d; // }else{ // this.x="0"; // this.style.backgroundColor=(this.sectionRowIndex%2==0)?a:b; // } // } t[i].οnmοuseοver=function(){ if(this.x!="1")this.style.backgroundColor=c; } t[i].οnmοuseοut=function(){ if(this.x!="1")this.style.backgroundColor=(this.sectionRowIndex%2==0)?a:b; } } }
后台把查询结果解析成json格式字符窜:
public ActionForward getInit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
OperationBO operationBO = null;
JSONObject data = new JSONObject();
try {
response.setContentType("text/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
operationBO = (OperationBO) InteractionObjectFactory.getInstance()
.getInteractionObject("TP_OperationBO",
getAppContext(request));
String logId = request.getParameter("logId");
String operType = request.getParameter("operType");
//操作日志信息
Map map = operationBO.queryLogInfo(logId, operType);
List logList = (List) map.get("resultlist");
JSONArray logJsonList = JSONArray.fromObject(logList);
String logStrlist = logJsonList.toString();
try {
out = response.getWriter();
data.put("logStrlist", logStrlist);
data.put("keys", (String) map.get("keys"));
data.put("showNames", (String) map.get("showNames"));
out.println(data.toString());
} catch (JSONException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}