ibatis 动态查询

最近项目需要,做了一个动态查询。把查询结果返回到页面上显示。

 

要查询的字段和表名都是动态的,是在后台拼出来的。

 

可是在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;
    }
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值