/** * Single-table query * * CLASSNAME: Entity class name * Vals: Query properties * pnames: Condition name * Pvals: value of Condition*/@Override PublicList<object>Get(String classname,string[] vals,object[] pnames,object[] pvals) {if(Stringutils.isempty (className)) {Throw NewNullparameterexception ("the name of the entity class is not written"); } stringbuffer hql=NewStringBuffer (); if(Vals.length! =0) {hql.append ("SELECT"); for(intI=0; i<vals.length;i++) {hql.append ("p."); Hql.append (Vals[i]); if(I! = vals.length-1) {hql.append (", "); }}} hql.append (" from"); Hql.append (ClassName); Hql.append ("P"); if(pnames.length>0&& pvals.length>0&& pnames.length==pvals.length) {hql.append ("WHERE"); for(intI=0; i<pnames.length;i++) {hql.append ("p."); Hql.append (Pnames[i]); Hql.append ("="); Hql.append (Pvals[i]); if(i<pnames.length-1) {hql.append (" and"); }}} System. out. println (Hql.tostring ()); return NULL; }
1. Userdao.Get("Verificationcode",NewString[] {"Val1","Val2"},Newobject[]{"P1","P2","P3"},NewObject[] {"Pv1","Pv2","Pv3"});
Output: SELECT p.val1, p.val2 from Verificationcode p WHERE P.p1=pv1 and P.p2=pv2 and P.p3=pv3
2. Userdao.get ("Verificationcode", new string[] {}, new object[]{"P1", "P2", "P3"}, new object[] {"Pv1", "Pv2", "Pv3"});
Output: From Verificationcode P WHERE P.p1=pv1 and P.p2=pv2 and P.p3=pv3
3. Userdao.get ("Verificationcode", new string[] {}, New object[]{}, new object[] {});
Output: from Verificationcode p
4.userdao.get ("Verificationcode", new string[] {"P1"}, New object[]{}, new object[] {});
Output: SELECT p.p1 from Verificationcode p
A small piece of code to assemble SQL