Address: http://kuangbaoxu.javaeye.com/blog/193076
1. query all fields of the entire ing object
// Directly from is a ing object, that is, querying all fields of the entire ing object <br/> string hql = "from users "; <br/> query = session. createquery (hql); </P> <p> List <users> Users = query. list (); <br/> for (users user: Users) {<br/> system. out. println (user. getname () + ":" + User. getpasswd () + ":" + User. GETID (); <br/>}</P> <p> the output result is: <br/> name1: password1: 1 <br/> name2: password2: 2 <br/> name3: password3: 3
2. query a single Field
// Query a single field <br/> string hql = "Select name from users"; <br/> query = session. createquery (hql); </P> <p> List <string> List = query. list (); <br/> for (string STR: List) {<br/> system. out. println (STR); <br/>}< br/> the output result is: <br/> name1 <br/> name2 <br/> name3
3. query several fields
// Query several fields <br/> string hql = "Select name, passwd from users"; <br/> query = session. createquery (hql); <br/> // by default, the queried list contains an array of objects. <br/> List <object []> List = query. list (); <br/> for (object [] object: List) {<br/> string name = (string) object [0]; <br/> string passwd = (string) object [1]; </P> <p> system. out. println (name + ":" + passwd); <br/>}< br/> the output result is: <br/> name1: password1 <br/> name2: password2 <br/> name3: password3
4. Modifying the default query result (query. List () is not returned in the form of an object [] array, but in the form of a list
// Query several fields and add new list (). Note that l in list is in lower case. You do not need to import the package. the List () is no longer the default object array, but the list is a collection <br/> string hql = "select new list (name, passwd) from Users "; <br/> query = session. createquery (hql); <br/> // The list queried by default stores an object array, but the list stores no longer the default object array, instead, the list is a collection of <br/> List <list> List = query. list (); <br/> for (list User: List) {<br/> string name = (string) user. get (0); <br/> string passwd = (string) user. get (1); </P> <p> system. out. println (name + ":" + passwd); <br/>}< br/>/** <br/> the output result is: <br/> name1: password1 <br/> name2: password2 <br/> name3: password3 <br/> */
5. Modify the default query result (query. List (). It is returned in the form of an array of object [] and a map.
// Query several fields and add new map (). Note that m in map is in lower case. You do not need to import the package. the List () is no longer the default object array, but the map set <br/> string hql = "select new map (name, passwd) from Users "; <br/> query = session. createquery (hql); <br/> // The list queried by default stores an object array, but the list stores no longer the default object array, instead, map sets <br/> List <map> List = query. list (); <br/> for (map user: List) {<br/> // all the field values in a record are an element in map, key is a string 0, 1, 2, 3 ...., value is the field value <br/> // if you change hql to: String hql = "select new map (name as username, passwd as password) from users ";, then the key will not be a string 0, 1, 2... but "username", "password" <br/> string name = (string) user. get ("0"); // get ("0"); is get (key), note: 0, 1, 2... is a string, not an integer <br/> string passwd = (string) user. get ("1"); </P> <p> system. out. println (name + ":" + passwd); <br/>}< br/>/** <br/> the output result is: <br/> name1: password1 <br/> name2: password2 <br/> name3: password3 <br/> */
6. modify the default query result (query. list () is returned in the form of a set instead of an object [] array. However, because there are no repeated elements in the Set, the values of username and password cannot be the same. Change hql to string hql = "select new set (name, passwd) from users ";
7. Modify the default query result (query. List (). The result is not returned in the form of an object [] array and is returned in a custom type.
Custom class:
package COM. domain; </P> <p> public class myuser {</P> <p> private string username; <br/> private string password; <br/> // because: string hql = "select new COM. domain. myuser (name, passwd) from users "; therefore, you must have a constructor that accepts two parameters. <br/> Public myuser (string username, string password) {<br/> This. username = username; <br/> This. password = password; <br/>}</P> <p> Public String GetUserName () {<br/> return username; <br/>}< br/> Public void setusername (string username) {<br/> This. username = username; <br/>}< br/> Public String GetPassword () {<br/> return password; <br/>}< br/> Public void setpassword (string password) {<br/> This. password = password; <br/>}</P> <p >}
// Query. in the list (), the default object array is no longer stored, but the custom class myuser. The package name must be added, string hql = "from users "; the users class in must also be added with the package name, but because of the users. HBM. in XML, the default value of <pibernate-mapping auto-import = "true"> auto-import is true (so the auto-import attribute can be left empty ), <br/> string hql = "select new COM. domain. myuser (name, passwd) from users "; <br/> query = session. createquery (hql); <br/> // The list queried by default stores an object array, but the list stores no longer the default object array, instead, the myuser object <br/> List <myuser> myusers = query. list (); <br/> for (myuser: myusers) {<br/> string name = myuser. getUserName (); <br/> string passwd = myuser. getPassword (); <br/> system. out. println (name + ":" + passwd); <br/>}< br/>/** <br/> the output result is: <br/> name1: password1 <br/> name2: password2 <br/> name3: password3 <br/> */
8: Conditional Query
// Condition query. The index value starts from 0 and the index location. Use setstring and setparameter to set the parameter <br/> string hql = "from users where name =? And passwd =? "; <Br/> query = session. createquery (hql); <br/> // 1st methods <br/> // query. setstring (0, "name1"); <br/> // query. setstring (1, "password1"); <br/> // 2nd query methods <br/>. setparameter (0, "name1", hibernate. string); <br/> query. setparameter (1, "password1", hibernate. string); <br/> List <users> List = query. list (); <br/> for (users: List) {<br/> system. out. println (users. GETID (); <br/>}
// condition query, custom index name (parameter name): username,: password. use setstring and setparameter to set the parameter <br/> string hql = "from users where name =: username and passwd =: Password"; <br/> query = session. createquery (hql); <br/> // 1st methods <br/> // query. setstring ("username", "name1"); <br/> // query. setstring ("password", "password1"); <br/> // 2nd methods, 3rd parameter types <br/> query. setparameter ("username", "name1", hibernate. string); <br/> query. setparameter ("password", "password1", hibernate. string); <br/> List <users> List = query. list (); <br/> for (users: List) {<br/> system. out. println (users. GETID (); <br/>}
// condition query, set the parameter <br/> string hql = "from users where name =: username and passwd =: Password"; <br/> query = session. createquery (hql); <br/> // The two attributes of the myuser class must correspond to: username and: Password <br/> myuser = new myuser ("name1 ", "password1"); <br/> query. setproperties (myuser); <br/> List <users> List = query. list (); <br/> for (users: List) {<br/> system. out. println (users. GETID (); <br/>}