Underlying method encapsulation: Fuzzy query, surname Zhang
Testservice.java
PackageJUnit; Public classTestservice {@Test Public voidSave () {ApplicationContext AC=NewClasspathxmlapplicationcontext ("Beans.xml"); Ielectextservice Electextservice=(Ielectextservice) Ac.getbean (ielectextservice.service_name); Electext e=NewElectext (); E.settextname ("ABBBC"); E.settextdate (NewDate ()); E.settextremark ("Deeef"); Electextservice.saveelectext (e); }
@Test Public void findcollectionbyconditionnopage (){applicationcontext AC=NewClasspathxmlapplicationcontext ("Beans.xml"); Ielectextservice Electextservice=(Ielectextservice) Ac.getbean (ielectextservice.service_name); Electext Electext=NewElectext (); Electext.settextname (Single); Electext.settextremark (Single); List<ElecText> list =electextservice.findcollectionbyconditionnopage (Electext); if(List! =NULL&& list.size () > 0){ for(Electext text:list) {System.out.println (Text.gettextname ()+ "+"+Text.gettextremark ()); } } }}
Electextservice.java Interface:
Package Com.itheima.elec.service; Public Interface Ielectextservice { publicstaticfinal String service_name= " Com.itheima.elec.service.impl.ElecTextServiceImpl "; void Saveelectext (Electext electext); List<ElecText> findcollectionbyconditionnopage (Electext electext);}
Electextserviceimpl.java interface
PackageCom.itheima.elec.service.impl;//transaction control: Spring's declarative transaction, adding @transactional at the service layer@Service (ielectextservice.service_name) @Transactional (readOnly=true) Public classElectextserviceimplImplementsIelectextservice {@Resource (name=ielectextdao.service_name) Ielectextdao Electextdao; @Transactional (ReadOnly=false) Public voidSaveelectext (Electext electext) {//TODO auto-generated Method StubElectextdao.save (Electext); } @Override PublicList<electext>findcollectionbyconditionnopage (Electext electext) {//Query CriteriaString condition = ""; //parameters corresponding to the query criteriaList<object> paramslist =NewArraylist<object>();//if (electext.gettextname () = null &&!electext.gettextname (). Equals ("")) {//condition + = "and o.textname like?";// } if(Stringutils.isnotblank (Electext.gettextname ())) {condition+ = "and o.textname like?"; Paramslist.add ("%" + electext.gettextname () + "%"); } if(Stringutils.isnotblank (Electext.gettextremark ())) {condition+ = "and o.textremark like?"; Paramslist.add ("%" + electext.gettextremark () + "%"); } //Passing variable parametersObject [] params =Paramslist.toarray (); //Sortmap<string, string> =NewLinkedhashmap<string,string>(); Orderby.put ("O.textdate", "ASC"); Orderby.put ("O.textname", "desc"); //Enquirylist<electext> list = electextdao.findcollectionbyconditionnopage (Condition,params,orderby); returnlist; }}
Icommondao.java
Package Com.itheima.elec.dao; Public Interface Icommondao<t> { void Save (T entity); void Update (T entity); T Findobjectbyid (Serializable ID); void deleteobjectbyids (Serializable ... ids); void deleteobjectbycollection (list<t> List); List<T> findcollectionbyconditionnopage (String condition, object[] params, map<string, string> );}
Icommondaoimpl.java
PackageCom.itheima.elec.dao.impl; Public classCommondaoimpl<t>extendsHibernatedaosupportImplementsIcommondao<t> { //Generic ConversionsClass Entityclass = Tutils.getactualtype ( This. GetClass ()); /*** How to implement this Save method: through Hibernatedaosupport, need to inject sessionfactory*/@Resource Public voidsetdi (sessionfactory sessionfactory) { This. Setsessionfactory (sessionfactory); } @Override Public voidSave (T entity) { This. Gethibernatetemplate (). Save (entity); } Public voidUpdate (T entity) { This. Gethibernatetemplate (). Update (entity); } @Override PublicT Findobjectbyid (Serializable id) {//Class entityclass = Tutils.getactualtype (This.getclass ()); returnT This. Gethibernatetemplate (). Get (Entityclass, id);//Entityclass type required here} @Override Public voiddeleteobjectbyids (Serializable ... ids) {if(IDs! =NULL&& ids.length > 0){ for(Serializable id:ids) {Object entity= This. Findobjectbyid (ID); This. Gethibernatetemplate (). Delete (entity); } }//this.gethibernatetemplate (). Delete (entity);} @Override Public voidDeleteobjectbycollection (list<t>list) { This. Gethibernatetemplate (). DELETEALL (list); } @Override/**The purpose of this 1=1 is to facilitate the assembly of SQL or HQL statements in the service layer, the connection is unified using and * SELECT o from electext o WHERE 1=1 #Dao层填写 And o.textname like '% Zhang% ' #Service拼装 and o.textremark like '% Zhang% ' #Service拼装 ORDER by O.textdate asc,o.textname desc #Service拼装*/ PublicList<t>Findcollectionbyconditionnopage(String condition, object[] params, map<string, string>) { //HQL Statements String hql = "from" + entityclass.getsimplename () + "o where 1 = 1"; //sort the fields stored in the map collection, organized into order by O.textdate ASC, O.textname Desc String orderbycondition = this. orderbyhql; //Add a query condition String finalhql = hql + condition + orderbycondition; //query, execute SQL statement//method One:list<t> list = This. Gethibernatetemplate (). Find (finalhql, params); returnlist; } PrivateString orderbyhql (map<string, string>) {StringBuffer buffer=NewStringBuffer (""); if(!! =NULL&& orderby.size () > 0) {buffer.append ("ORDER by"); for(Map.entry<string, string>Map:orderby.entrySet ()) {Buffer.append (Map.getkey ()+ "" + map.getvalue () + ","); } //Delete last commaBuffer.deletecharat (Buffer.length ()-1); } returnbuffer.tostring (); }}
2017.1.11 11:58
SSH Power Project Three