1、serivce类:
<span style="font-size:14px;">
@Service
public class EffectService {
@Autowired
private FindInfoClassDao findInfoClassDao;
@Autowired
private EffectDao effecQueryDao;
public Pageable<Map<String,Object>> condictionSearch(String tableId,Map params,Page page) throws ParseException{
LogicTable table =findInfoClassDao.findInfoClassTable(tableId);
List<LogicColumn> searchColumn = findInfoClassDao.
findColumnsByTableId(tableId,"1","0");
final List<LogicColumn> resultColumn =
findInfoClassDao.findColumnsByTableId(tableId,"0","1");
Set parNames = params.keySet();
final StringBuffer querySql = new StringBuffer("SELECT * FROM " + table.getName() + " WHERE 1=1 ");
final StringBuffer countSql = new StringBuffer("SELECT count(*) FROM " + table.getName() + " WHERE 1=1 ");
StringBuffer conditionSql = new StringBuffer(" ");
for (Object obj : parNames) {
String name = obj.toString();
String value = ((String[])params.get(obj))[0];
if(StringUtils.isNoneBlank(value)){
for (LogicColumn c : searchColumn) {
if(StringUtils.equals(c.getName(), name)){
if(StringUtils.equals(c.getType(),"number") ||
StringUtils.equals(c.getType(),"int")){
conditionSql.append(" AND "+name+" = "+value);
}else if(StringUtils.equals(c.getType(),"varchar2")){
conditionSql.append(" AND "+name+" = "+"'"+value+"'");
}else if(StringUtils.equals(c.getType(),"date")){//前台日期格式:2014-09-09
Date startDate = DateUtils.parseDate(value, "yyyy-MM-dd 00:00:00");
Date endDate = DateUtils.parseDate(value, "yyyy-MM-dd 23:59:59");
// conditionSql.append(" AND TO_CHAR("+c.getName()+",'yyyy-mm-dd') = '"+ value+"'");
conditionSql.append(" AND "+c.getName() +">="+startDate+" AND "+c.getName() +"<="+endDate);
}
}
}
}
}
//执行查询
Pageable<Map<String,Object>> pages =
effecQueryDao.findWithPage(countSql.append(conditionSql).toString(),
querySql.append(conditionSql).toString(),
null, new RowMapper<Map<String,Object>>() {
@Override
public Map<String,Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
Map<String,Object> map = new HashMap<String,Object>();
for (LogicColumn column : resultColumn) {
//System.out.println("查询sql:=="+querySql);
//System.out.println("总数sql:=="+countSql);
column.getType();
System.out.println("column.getName():=="+column.getName()+","+column.getNameDesc());
// System.out.println(column.getType());
if(StringUtils.equals(column.getType(),"int") ||
StringUtils.equals(column.getType(),"number")){
map.put(column.getName(), rs.getInt(column.getName()));
}else if(StringUtils.equals(column.getType(),"varchar2")){
// System.out.println(column.getType());
map.put(column.getName(), rs.getString(column.getName()));
}else if(StringUtils.equals(column.getType(),"date")){//前台日期格式:2014-09-09
map.put(column.getName(), rs.getDate(column.getName()));
}
}
return map;
}
}, page);
return pages;
}
}
</span>
二、controller:
<span style="font-size:14px;">/**
* 条件动态查询信息类
* @param request
* @param response
* @param writer
* @throws Exception
*/
@RequestMapping("/conditionSearch")
public void conditionSearch(HttpServletRequest request,
HttpServletResponse response, Writer writer) throws Exception {
String tableId = request.getParameter("tableId");
Map params = request.getParameterMap();
System.out.println("map 的参数:"+params);
Pageable<Map<String,Object>> page =
effectService.condictionSearch(tableId,params,RequestUtils.buildPage(request));
String json = ResponseUtils.buildJson(page);
writer.write(json);
}</span>
三、easyui生成 jsp页面:
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>有效数据</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/jquery-easyui-1.3.4/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/jquery-easyui-1.3.4/themes/icon.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/jquery-easyui-1.3.4/themes/icon-suit-a.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-easyui-1.3.4/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-easyui-1.3.4/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/base.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/My97DatePicker/WdatePicker.js"></script>
</head>
<script type="text/javascript">
$(function(){
$('#deptNameBox').combobox('setValue','${ sysUser.sysGroupName }');
});
</script>
<body>
<div style="margin:10px 10px;"></div>
<table id="customGrid" class="easyui-datagrid"
style="height:600px;width:1024px"
data-options="rownumbers:true, pagination:true,
singleSelect:true, collapsible:true, fitColumns:true,
pageSize:10, url:'', title:'请选择下拉框查询',
iconCls:'', toolbar:'#tb'">
</table>
<div id="tb" style="padding:5px;height:auto">
<div style="margin-bottom:5px">
部门:<input id="deptNameBox" class="easyui-combobox" name=""
data-options="
required:true,
editable:false,
url:'${pageContext.request.contextPath}/front/table/findDept.action?uid=${ sysUser.username }',
method:'post',
valueField:'id',
textField:'text',
panelHeight:'auto',
onSelect:function(rec){
var url = '${pageContext.request.contextPath}/front/table/findTableByDept.action?id='+rec.id;
$('#tableNameBox').combobox('clear');
$('#tableNameBox').combobox('reload', url);
}
">
信息类:<input id="tableNameBox" class="easyui-combobox" name=""
data-options="
required:true,
editable:false,
valueField:'id',
textField:'text',
panelHeight:'auto',
onSelect:function(rec){
var url = '${pageContext.request.contextPath}/front/table/findQueryColumnByTable.action?issearch=1&id='+rec.id;
var flag = 1;
jQuery.post(url,function(data){
var conditionHtml = '';
jQuery.each(data,function(i,item){
if(item.attributes == 'date'){
conditionHtml += '<span style=\'padding-left:5px\'>'+item.text+'</span>:<input name=\''+item.state+'\' id=\''+item.id+'\' class=\'Wdate\' onFocus=\'WdatePicker({readOnly:true})\' style=\'width:100px\'>';
}else{
conditionHtml += '<span style=\'padding-left:5px\'>'+item.text+'</span>:<input name=\''+item.state+'\' id=\''+item.id+'\' style=\'width:100px\'>';
}
flag = 0;
});
jQuery('#searchCondition').html('').html(conditionHtml);
flag == 0 ? jQuery('#searchForm').show() : jQuery('#searchForm').hide();
$('#customGrid').datagrid('loadData',{total:0,rows:[]});
$('#customGrid').datagrid({columns:[[]],url:''});
},'json');
}
">
<form id="searchForm" style="display:none;padding-top: 10px;">
<span id="searchCondition"></span>
<a href="#" class="easyui-linkbutton" iconCls="icon-search" οnclick="conditionSearch();">查询</a>
<a href="#" class="easyui-linkbutton" iconCls="icon-refresh" οnclick="conditionReset();">重置</a>
</form>
</div>
</div>
<script type="text/javascript">
function viewFormatter(value,row,index){
var id = row.customId;
var name = row.customName;
return '<a href="#" οnclick="viewDetail(\''+id+'\',\''+name+'\');">查看</a>';
}
function viewDetail(id,name){
jQuery.post('${pageContext.request.contextPath}/query/custom/findById.action',{"customId":id},
function(data){
$('#customViewGrid').datagrid({"data":data});
$('#customviewWin').window({'title':"定制名称:"+name});
$('#customviewWin').window('open');
},
'json'
);
}
function conditionSearch(){
var tableId = jQuery('#tableNameBox').combobox('getValue');
var inputs = jQuery('#searchCondition').find('input');
jQuery.ajax({
url:'${pageContext.request.contextPath}/front/table/findQueryColumnByTable.action',
data:{'id':tableId,'isresult':'1'},
async:false,
dataType:'json',
success:function(data){
reloadGrid(data);
}
});
function reloadGrid(data){
var columns = [];
var columnName = '';
jQuery.each(data,function(i,item){
var obj = {'field':item.state,'title':item.text};
columns.push(obj);
columnName += item.state +',';
});
//查询参数
var querParams = {};
jQuery.each(inputs,function(i,item){
querParams[item.name] = item.value;
});
$('#customGrid').datagrid({
method:'post',
queryParams:querParams,
url:'${pageContext.request.contextPath}/query/effecQuery/conditionSearch.action?id='+tableId+'&columnName='+columnName,
columns:[columns],
});
}
}
//重置
function conditionReset(){
jQuery.each(jQuery('#searchForm input'),function(i,item){
jQuery(item).val('');
});
}
</script>
</body>
</html></span>