java中多條件與不定條件查詢


java中多條件與不定條件查詢 

網站或各類管理系統都會用到搜索,會用到一個或多個不確定條件搜索,單條件搜索比較簡單,有時候會有多個條件共同查詢,如果系統中已經提供了相關的方法供你使用最好,像我做這老系統改版,需要添加搜索,就要自己寫了。開始也沒管那么多,就是拼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 class MultiTaskSearch {
    
    @Resource(name="jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Resource(name = "storeFrontDAO")
    
    private Map search() {
        String name="公司";
        String email="@163";
        String invoiceTitle="公司";
        int sign=1;
        String at="2012-04-26";
        Map<String, Object> map=new LinkedHashMap<String, Object>();//保持添加順序
//        Map<String, Object> map=new HashMap<String, Object>();//無固定順序
        map.put("name like", name);
        map.put("email like", email);
        map.put("invoiceTitle like", invoiceTitle);
        map.put("sign =", sign);
        map.put("addtime>=", at);
        return map;
    }
    
    public <T> List<T> dbSearch(Class typeClass,Map<String, Object> map,String orderby) {
        String paths[] = { "ams-servlet.xml" };
        ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
        jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
        
        List<T> TList=null;
        String tablename = typeClass.getName().substring(
                typeClass.getName().lastIndexOf(".") + 1);
        StringBuffer sql=new StringBuffer("select * from ");
        sql.append(tablename);
        if (map.size()!=0) {
            sql.append(" 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()+" ");
                    sql.append(" and t."+mapEntry.getKey()+" '%"+mapEntry.getValue()+"%'");
                //精確匹配
                }else {
//                sql.append(" and t."+mapEntry.getKey()+" '%"+mapEntry.getValue()+"%'");
                    sql.append(" and t."+mapEntry.getKey()+" "+mapEntry.getValue()+" ");
                }
            }
        }
        if (null!=orderby&&!"".equals(orderby)) {
            sql.append(orderby);
        }
        System.out.println("SQL:"+sql.toString());
        TList=jdbcTemplate.query(sql.toString(),new Object[] {}, new BeanPropertyRowMapper<T> (typeClass));
        
        return TList;
    }
    public static void main(String[] args) {
        MultiTaskSearch mt=new MultiTaskSearch();
        Map map=mt.search();
        String orderby=" order by addTime desc";
        List<Agent> agents=mt.dbSearch(Agent.class, map,orderby);
        for (Agent agent : agents) {
            System.out.println(agent.getName());
        }
        System.out.println("****************"+agents.size());
        
    }
}
或者可以用拼sql的方法實現
使用union關鍵字可以在一個文本框內搜索出多個條件的數據
select  t1.* from
(
select *  from agent where name like '"2%' 
union 
select *  from agent where email like '"2%' 
union 
select *  from agent where ContactPerson like '"2%'
) t1
這種查詢結果是所有的集合,也可以把union換成or
 
我個人用於測試的,僅供參考,如果讀者覺得有問題,可以給我留言交流.
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM