Conditional query
1, the first kind, with? Placeholders, such as://login (with the? placeholder) PublicList<userpo>Loginuser (Userpo up) throws exception{session session=hibernatesessionfactory.getsession (); String hql="From userpo where name =? and pwd=?"; Query Query=session.createquery (HQL); Query.setstring (0, Up.getname ()); Query.setstring (1, Up.getpwd ()); List<UserPO> list =query.list (); Session.close (); returnlist;}2, with ": +named "placeholders, such as://Login (with ": Name" placeholder) PublicList<userpo>LoginUser2 (Userpo up) throws exception{session session=hibernatesessionfactory.getsession (); String hql="From userpo WHERE name =: N and pwd=:p"; Query Query=session.createquery (HQL); Query.setstring ("N", Up.getname ()); Query.setstring ("P", Up.getpwd ()); List<UserPO> list =query.list (); Session.close (); returnlist;} 2.1, you can also use this placeholder to set values such as://Login (with ": Name" placeholder, set value with Setparameter) PublicList<userpo>LoginUser3 (Userpo up) throws exception{session session=hibernatesessionfactory.getsession (); String hql="From userpo WHERE name =: N and pwd=:p"; Query Query=session.createquery (HQL); Query.setparameter ("N", Up.getname ()); Query.setparameter ("P", Up.getpwd ()); List<UserPO> list =query.list (); Session.close (); returnlist;} Using this method does not need to state the type of mapping, Hibernate will automatically go through the configuration, but since Hibernate has two date formats: Date and timestamp, you must specify the type of the mapping for the date type. Wording:3, parameter binding according to the object, such as://Login (with ": Name" placeholder, set the value with SetProperties, the name parameter must be the same as the property name being bound) PublicList<userpo>LoginUser4 (Userpo up) throws exception{session session=hibernatesessionfactory.getsession (); String hql="From userpo WHERE name =: Name and pwd=:p WD"; Query Query=session.createquery (HQL); Query.setproperties (UP); List<UserPO> list =query.list (); Session.close (); returnlist;}4, use condition query (criteria), such as://Sign In (conditional query criteria) completely out of SQL statements and HQL statements PublicList<userpo>LoginUser5 (Userpo up) throws exception{session session=hibernatesessionfactory.getsession (); Criteria CRI= Session.createcriteria (Userpo.class); Cri.add (Restrictions.eq ("name", Up.getname ())); Cri.add (Restrictions.eq ("pwd", Up.getpwd ())); List<UserPO> list =cri.list (); Session.close (); returnlist;} 5, offline conditional queries, such as://Login (query Detachedcriteria with offline conditions) PublicList<userpo>LoginUser6 (Userpo up) throws exception{session session=hibernatesessionfactory.getsession (); Detachedcriteria DC= Detachedcriteria.forclass (Userpo.class); Dc.add (Restrictions.eq ("name", Up.getname ())); Dc.add (Restrictions.eq ("pwd", Up.getpwd ())); Criteria CRI=Dc.getexecutablecriteria (session); List<UserPO> list =cri.list (); Session.close (); returnlist;} Using offline you can write it in the business layer and pass it in as a parameter to reduce the DAO's code. 6, paged query: The paging query is the processing method in the database application, and the query and Criteria interfaces provide a method for paging queries:1) Setfirstresult (int): Specifies which object to start the query from, and the parameter is the index position, starting at 0. 2) Setmaxresult (int): Specifies the maximum number of objects to query at one time.
Query all records:
/** * Check all the records*/ Public StaticList SelectAll () {List List=NULL; Try{Session=hibernatesessionfactory.getsession (); Transaction Tran=session.begintransaction (); Query Q= Session.createquery ("From User1"); List=q.list (); Tran.commit (); } Catch(Exception e) {e.printstacktrace (); } finally{hibernatesessionfactory.closesession (); } returnlist;}
Hibernate conditional query in several ways + query all records