Website or all kinds of management system will be used to search, will use one or more uncertain conditions search, single-condition search is relatively simple, sometimes there will be multiple conditions common query, if the system has provided the relevant method for you to use the best, like I do this old system revision, need to add search, you have to write. Start also did not control so much, is to spell SQL, but later found to add a lot of search places, always so write not only increase the workload, but also do a lot of repetitive work, maybe later will do such work, so or write a more general Query method.
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= "Company"; &nbSp; string email= "@163"; String invoicetitle= "Company"; int sign=1; string at= "2012-04-26"; Map<String, Object> map=new LinkedHashMap<String, Object> ()///Keep adding order// Map<String, Object> map=new HashMap< String, object> ();//no fixed order 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");//after only stitching and conditions } 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 ())) { //Fuzzy Matching if (Mapentry.getkey (). ToString (). Contains ("like") {// sql.append (" and t.") +mapentry.getkey () + " " +mapentry.getvalue () + " "); sql.append (" and t.") +mapentry.getkey () + " '%" +mapentry.getvalue () + "% '"); //Exact Match }else {// sql.append (" and t.") +mapentry.getkey () + " '%" +mapentry.getvalue () + "% '"); sql.append (" and t.") +mapentry.getkey () + " " +mapentry.getvalue () + " "); } } } if (null!=orderby&&! "". Equals (to Be)) { sql.append ( ; } ) System.out.println ("SQL:" +sql.tostring ()); &NBSP;&NBSP;&NBSP;&NBSp; 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的方法实现Use the Union keyword to search for multiple criteria data within a text box select t1.* from (SELECT * from agent where name "" 2% ' union SELECT * from Agent where Email like ' 2% ' union SELECT * from agent where ContactPerson ' "2% ') T1 This query result is all set, can also be converted to or I personally used for testing, for reference only, if Readers feel that there is a problem, you can give me a message exchange.
Multi-condition and indefinite condition query in Java