1. Querying all fields of the entire mapping object
The direct from query is a mapping object, that is: querying the entire Mapping object all fields
String hql = "from Users";
Query query = session.createquery (HQL);
list<users> Users = Query.list ();
for (Users user:users) {
System.out.println (user.getname () + ":" + user.getpasswd () + ":" + User.getid ());
}
the output is:
name1:password1:1
name2:password2:2
name3:password3:3
2. Querying a single field
Query single field
String hql = "SELECT name from Users";
Query query = session.createquery (HQL);
list<string> list = Query.list ();
for (String str:list) {
System.out.println (str);
}
The output is:
name1
name2
Name3
3. Query several of these fields (by default it is stored in an object array)
Query several of these fields
String hql = "Select name,passwd from Users";
Query query = session.createquery (HQL);
The default query list contains an Object array
list<object[]> list = Query.list ();
For (object[] object:list) {
String name = (string) object[0];
String passwd = (string) object[1];
SYSTEM.OUT.PRINTLN (name + ":" + passwd);
}
The output is:
name1:password1
name2:password2
name3:password3
4. Modify default Query Results (query.list ()) not returned as an array of object[], returned in list form
Query several of these fields, add new list (), and note that the L in list is lowercase. There is no need to import the package, so the list stored by query.list () is no longer the default object array, but the list is a collection of
String hql = "Select New List (NAME,PASSWD) from Users ";
Query query = session.createquery (HQL);
The default query list contains an object array, but here the list is no longer the default object array, but the list is a collection of
list<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 is:
name1:password1
name2:password2
name3:password3
*/
5. Modify default Query Results (query.list ()) not returned as an array of object[], returned in map form
Query several of these fields, add new Map (), and note that the M in the map is lowercase. There is no need to import the package, so the list stored by query.list () is no longer the default object array, but the map sets the String hql = "Select New Map (NAME,PASSWD) from
Users ";
Query query = session.createquery (HQL);
The default query list contains an object array, but here the list is no longer the default object array, but a Map collection of list<map> List = Query.list ();
for (Map user:list) {//A record all field values are an element in the MAP, key is the 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", then key will not be string 0,1,2 ... but "Userna
Me "," password "the String name = (string) user.get (" 0 ");//get (" 0 "); is get (key), note: 0,1,2 ... is a string, not an orthopedic
String passwd = (string) user.get ("1");
SYSTEM.OUT.PRINTLN (name + ":" + passwd); The/** output is: Name1:password1 name2:password2 name3:pa SSWORD3 * *
6. Modify default Query Results (query.list ()) not returned in object[] array, returned as a custom type
Custom class Definitions
Package com.domain;
public class MyUser {
private String username;
private String password;
because: String hql = "Select New com.domain.MyUser (NAME,PASSWD) from Users", there must be a constructor that accepts 2 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;
}
}
The list stored by query.list () is no longer the default object array, but a custom class myuser, which must be added with the package name, String hql = "from users", and the users class must be added to the package name. But because again Users.hbm.xml
7: Conditional Query
Conditional query, parameter index value starting at 0, index position. Set parameters by Setstring,setparameter
String hql = "from Users where name=? and passwd=? ";
Query query = session.createquery (HQL);
1th Way
// query.setstring (0, "name1"); query.setstring (1, "Password1");
2nd Way
query.setparameter (0, "name1", hibernate.string);
Query.setparameter (1, "Password1", hibernate.string);
list<users> list = Query.list ();
for (Users users:list) {
System.out.println (Users.getid ());
}
Or you can use the SetProperty (object ob) method of query to pass in the object.
Conditional query, set parameters by SetProperties
String hql = "from Users where Name=:username and passwd=:p assword";
Query query = session.createquery (HQL);
The 2 attributes of the MyUser class must be the same as: username and: password corresponds to
MyUser MyUser = new MyUser ("Name1", "Password1");
Query.setproperties (myUser);
list<users> list = Query.list ();
for (Users users:list) {
System.out.println (Users.getid ());
}
This article turns from http://blog.csdn.net/xinyu0100/article/details/5385989