1. query all fields of the entire ing object
Java code
- // The direct from query shows a ing object, that is, querying all fields of the entire ing object.
- String hql = "from users ";
- Query query = session. createquery (hql );
- List Users = query. List ();
- For (users user: Users ){
- System. Out. println (user. getname () + ":" + User. getpasswd () + ":" + User. GETID ());
- }
- Output result:
- Name1: password1: 1
- Name2: password2: 2
- Name3: password3: 3
// Directly from is a ing object, that is, query all the fields of the entire ing object string hql = "from users"; query = session. createquery (hql); List
Users = query. list (); For (users user: Users) {system. out. println (user. getname () + ":" + User. getpasswd () + ":" + User. GETID ();} the output result is: name1: password1: 1 name2: password2: 2 name3: password3: 3
2. query a single Field
Java code
- // Query a single Field
- String hql = "Select name from users ";
- Query query = session. createquery (hql );
- List List = query. List ();
- For (string STR: List ){
- System. Out. println (STR );
- }
- Output result:
- Name1
- Name2
- Name3
// Query a single field string hql = "Select name from users"; query = session. createquery (hql); List
List = query. List (); For (string STR: List) {system. Out. println (STR);} the output result is name1 name2 name3.
3. query several fields
Java code
- // Query several fields
- String hql = "Select name, passwd from users ";
- Query query = session. createquery (hql );
- // By default, the queried list stores an object array.
- ListList = query. List ();
- For (object [] object: List ){
- String name = (string) object [0];
- String passwd = (string) object [1];
- System. Out. println (name + ":" + passwd );
- }
- Output result:
- Name1: password1
- Name2: password2
- Name3: password3
// Query several of the fields string hql = "Select name, passwd from users"; query = session. createquery (hql); // The list queried by default stores an object array list.List = query. list (); For (object [] object: List) {string name = (string) object [0]; string passwd = (string) object [1]; system. out. println (name + ":" + passwd);} output: name1: password1 name2: password2 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
Reference
// Query several fields and add new list (). Note that l in list is in lower case. You do not need to import the package. In this way, the default object array is no longer stored in the list through query. List (), but the list set.
String hql = "select new list (name, passwd) from users ";
Query query = session. createquery (hql );
// By default, the queried list stores an object array, but the list is not the default object array, but a list set.
List List = query. List ();
For (list User: List ){
String name = (string) user. Get (0 );
String passwd = (string) user. Get (1 );
System. Out. println (name + ":" + passwd );
}
/**
Output result:
Name1: password1
Name2: password2
Name3: password3
*/
5. Modifying the default query result (query. List () is not returned in the form of an object [] array, but in the form of a map.
Java code
- // Query several fields and add new map (). Note that m in map is in lower case. You do not need to import the package. In this way, the default object array is no longer stored in the list through query. List (), but the map set.
- String hql = "select new map (name, passwd) from users ";
- Query query = session. createquery (hql );
- // The list queried by default stores an object array, but the list stores the default object array instead of the map array.
- List
List = query. List ();
- For (map user: List ){
- // All the field values in a record are an element in map. The key is a string of 0, 1, 2, 3..., and the value is the field value.
- // If you change hql to string hql = "select new map (name as username, passwd as password) from users";, the key is not a string 0, 1, 2... but "username" and "password ".
- String name = (string) user. get ("0"); // get ("0"); is get (key), note: 0, 1, 2... is a string, not an integer
- String passwd = (string) user. Get ("1 ");
- System. Out. println (name + ":" + passwd );
- }
- /**
- Output result:
- Name1: password1
- Name2: password2
- Name3: password3
- */
// Query several fields and add new map (). Note that m in map is in lower case. You do not need to import the package. in the list (), the default object array is no longer stored, but Map sets string hql = "select new map (name, passwd) from users "; query query = session. createquery (hql); // The list queried by default stores an object array, but the list stores a list instead of the default object array.List = query. list (); For (map user: List) {// all the field values in a record are an element in the map. The key is a string 0, 1, 2, 3 ...., value is the field value // if you change hql to: String hql = "select new map (name as username, passwd as password) from users";, the key is not the string 0, 1, 2... but "username", "password", string name = (string) user. get ("0"); // get ("0"); is get (key), note: 0, 1, 2... is a string, not an integer string passwd = (string) user. get ("1"); system. out. println (name + ":" + passwd);}/** the output result is: name1: password1 name2: password2 name3: password3 */
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:
Java code
- Package com. domain;
- Public class myuser {
- Private string username;
- Private string password;
- // Because: String hql = "select new COM. domain. myuser (name, passwd) from users"; therefore, you must have a constructor that accepts two parameters.
- Public myuser (string username, string password ){
- This. Username = username;
- This. Password = password;
- }
- Public String GetUserName (){
- Return username;
- }
- Public void setusername (string username ){
- This. Username = username;
- }
- Public String GetPassword (){
- Return password;
- }
- Public void setpassword (string password ){
- This. Password = password;
- }
- }
Package COM. domain; public class myuser {private string username; private string password; // because: String hql = "select new COM. domain. myuser (name, passwd) from users "; therefore, you must have a constructor that accepts two parameters: Public myuser (string username, string password) {This. username = username; this. password = password;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password ;}}
Java code
- // 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. XML The default value of auto-import is true (so the auto-import attribute can be left empty ).
- String hql = "select new COM. domain. myuser (name, passwd) from users ";
- Query query = session. createquery (hql );
- // By default, the queried list stores an object array, but the list stores no longer the default object array, but the myuser object.
- List Myusers = query. List ();
- For (myuser: myusers ){
- String name = myuser. GetUserName ();
- String passwd = myuser. GetPassword ();
- System. Out. println (name + ":" + passwd );
- }
- /**
- Output result:
- Name1: password1
- Name2: password2
- Name3: password3
- */
// 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. XML
The default value of auto-import is true (so the auto-import attribute can be left empty). String hql = "select new COM. domain. myuser (name, passwd) from users "; query = session. createquery (hql); // The list queried by default stores an object array, but the list stores a list of myuser objects instead of the default object array.
Myusers = query. list (); For (myuser: myusers) {string name = myuser. getUserName (); string passwd = myuser. getPassword (); system. out. println (name + ":" + passwd);}/** the output result is: name1: password1 name2: password2 name3: password3 */
8: Conditional Query
Java code
- // Condition query. The index value starts from 0 and the index location. Set parameters through setstring and setparameter
- String hql = "from users where name =? And passwd =? ";
- Query query = session. createquery (hql );
- // There are 1st Methods
- // Query. setstring (0, "name1 ");
- // Query. setstring (1, "password1 ");
- // There are 2nd Methods
- Query. setparameter (0, "name1", hibernate. String );
- Query. setparameter (1, "password1", hibernate. String );
- List List = query. List ();
- For (users: List ){
- System. Out. println (users. GETID ());
- }
// Condition query. The index value starts from 0 and the index location. Use setstring and setparameter to set the string hql = "from users where name =? And passwd =? "; Query = session. createquery (hql); // 1st Methods // query. setstring (0, "name1"); // query. setstring (1, "password1"); // query in 2nd modes. setparameter (0, "name1", hibernate. string); query. setparameter (1, "password1", hibernate. string); List
List = query. List (); For (users: List) {system. Out. println (users. GETID ());}
Java code
- // Condition query, custom index name (parameter name): username,: password. set parameters through setstring and setparameter
- String hql = "from users where name =: username and passwd =: Password ";
- Query query = session. createquery (hql );
- // There are 1st Methods
- // Query. setstring ("username", "name1 ");
- // Query. setstring ("password", "password1 ");
- // 2nd methods, 3rd parameters determine the type
- Query. setparameter ("username", "name1", hibernate. String );
- Query. setparameter ("password", "password1", hibernate. String );
- List List = query. List ();
- For (users: List ){
- System. Out. println (users. GETID ());
- }
// Condition query, custom index name (parameter name): username,: password. set the parameter string hql = "from users where name =: username and passwd =: Password" Through setstring and setparameter; query = session. createquery (hql); // 1st Methods // query. setstring ("username", "name1"); // query. setstring ("password", "password1"); // 2nd methods, 3rd parameters determine the type of query. setparameter ("username", "name1", hibernate. string); query. setparameter ("password", "password1", hibernate. string); List
List = query. List (); For (users: List) {system. Out. println (users. GETID ());}
Java code
- // For conditional query, set parameters through setproperties
- String hql = "from users where name =: username and passwd =: Password ";
- Query query = session. createquery (hql );
- // The two attributes of the myuser class must correspond to: username and: password.
- Myuser = new myuser ("name1", "password1 ");
- Query. setproperties (myuser );
- List List = query. List ();
- For (users: List ){
- System. Out. println (users. GETID ());
- }