网站或各类管理系统都会用到搜索,会用到一个或多个不确定条件搜索,单条件搜索比较简单,有时候会有多个条件共同查询,如果系统中已经提供了相关的方法供你使用最好,像我做这老系统改版,需要添加搜索,就要自己写了。开始也没管那么多,就是拼sql,但是后来发现要加搜索地方不少,总是这样写既增加了工作量,还要做很多重复工作,说不定以后还会做这样的工作,所以还是写一个比较通用的查询方法。

package com.test;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.poi.hssf.record.formula.functions.T;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import com.ams.bo.webapi.dto.Agent;
public MultiTaskSearch {
    
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Resource(name = "storeFrontDAO")
    
private Map search() {
"公司";
"@163";
"公司";
int sign=1;
"2012-04-26";
new LinkedHashMap<String, Object>();//保持添加顺序
//        Map<String, Object> map=new HashMap<String, Object>();//无固定顺序
"name like", name);
"email like", email);
"invoiceTitle like", invoiceTitle);
"sign =", sign);
"addtime>=", at);
return map;
    }
    
public <T> List<T> dbSearch(Class typeClass,Map<String, Object> map,String orderby) {
"ams-servlet.xml" };
new ClassPathXmlApplicationContext(paths);
jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
        
null;
        String tablename = typeClass.getName().substring(
".") + 1);
new StringBuffer("select * from ");
        sql.append(tablename);
if (map.size()!=0) {
" t where 1=1");//后面只需拼接and条件
        }
        Set<Entry<String, Object>> set=map.entrySet();
Iterator iterator=set.iterator();
for (int i = 0; i < set.size(); i++) {
Map.Entry mapEntry=(Entry) iterator.next();
if (!"".equals(mapEntry.getValue().toString())) {
                
//模糊匹配
if (mapEntry.getKey().toString().contains("like")) {
//                sql.append(" and t."+mapEntry.getKey()+" "+mapEntry.getValue()+" ");
" and t."+mapEntry.getKey()+" '%"+mapEntry.getValue()+"%'");
//精确匹配
else {
//                sql.append(" and t."+mapEntry.getKey()+" '%"+mapEntry.getValue()+"%'");
" and t."+mapEntry.getKey()+" "+mapEntry.getValue()+" ");
                }
            }
        }
if (null!=orderby&&!"".equals(orderby)) {
            sql.append(orderby);
        }
out.println("SQL:"+sql.toString());
jdbcTemplate.query(sql.toString(),new Object[] {}, new BeanPropertyRowMapper<T> (typeClass));
        
return TList;
    }
public static void main(String[] args) {
new MultiTaskSearch();
Map map=mt.search();
" order by addTime desc";
mt.dbSearch(Agent.class, map,orderby);
for (Agent agent : agents) {
out.println(agent.getName());
        }
out.println("****************"+agents.size());
        
    }
}

或者可以用拼sql的方法实现

使用union关键字可以在一个文本框内搜索出多个条件的数据

selectt1.* from
(
select*  fromagent wherename like '"2%' 
union 
select*  fromagent whereemail like '"2%' 
union 
select*  fromagent whereContactPerson like '"2%'
)t1
这种查询结果是所有的集合,也可以把union换成or

 

*代码不够完美,仅供参考