Looking at JDBC today, we find that there is a snippet of code that can reduce the number of repetitive methods of writing queries, as follows:
1 Publiclist<goddess> query (list<map<string, object>> params)throwsexception{2List<goddess> result=NewArraylist<goddess>();3 4Connection conn=dbutil.getconnection ();5StringBuilder sb=NewStringBuilder ();6Sb.append ("select * from imooc_goddess where 1=1");7 8 if(params!=NULL&¶ms.size () >0){9 for(inti = 0; I < params.size (); i++) {TenMap<string, object> map=Params.get (i); OneSb.append ("and" +map.get ("name") + "" +map.get ("RelA") + "" +map.get ("value") + ""); A } - } - thePreparedStatement ptmt=conn.preparestatement (sb.tostring ()); - - System.out.println (sb.tostring ()); -ResultSet rs=ptmt.executequery (); + -Goddess G=NULL; + while(Rs.next ()) { Ag=Newgoddess (); atG.setid (Rs.getint ("id")); -G.setuser_name (rs.getstring ("user_name")); -G.setage (Rs.getint ("Age")); -G.setsex (Rs.getint ("Sex"))); -G.setbirthday (rs.getdate ("Birthday")); -G.setemail (rs.getstring ("email")); inG.setmobile (Rs.getstring ("mobile")); -G.setcreate_date (Rs.getdate ("Create_date")); toG.setcreate_user (rs.getstring ("Create_user")); +G.setupdate_date (Rs.getdate ("Update_date")); -G.setupdate_user (rs.getstring ("Update_user")); theG.setisdel (Rs.getint ("Isdel")); * $ Result.add (g);Panax Notoginseng } - returnresult; the}
It uses list<map<string,object>> to pass the data.
Map.put (name, "XXX"); Query condition name
Map.put (rel, "XXX"); The conditional relationship of a query is greater than, less than, or equal to
Map.put (Value, "XXX"); condition value of the query
Use list can store more than one map, reduce for a particular function to write a query method, enhance the code to take!
Thank you-jdbc the girl across the school to see the course
How to set query conditions dynamically in JDBC