The first two articles introduced the acquisition of Sessionfactory,session, as well as the session of the three States and the transition between, this article is bound to be the most concerned about the issue to be introduced, That is, you are most concerned about the database through the hibernate provided by the classes and methods of a deeper query.
This article is mainly about query queries
1. Introduce the entity class first
public class User {
//id field, primary key
private int id;
User name
private String username;
Password
private String userpass;
Parameterless construction method Public
User () {}
///have parameter constructor public
user (int id,string username,string userpass) {
this.id= ID;
This.username=username;
This.userpass=userpass;
}
public int getId () {return
ID;
}
Appropriate getter setter public
void setId (int id) {
this.id = ID;
}
Public String GetUserName () {return
username;
}
public void Setusername (String username) {
this.username = username;
}
Public String Getuserpass () {return
userpass;
}
public void Setuserpass (String userpass) {
this.userpass = userpass;
}
}
2. After the introduction of entity classes, the data in the database to query 1 to get all the fields of the entire object
public static void Main (string[] args) {
//TODO auto-generated A stub session
Session=null;
Transaction Tx=null;
Session=hibernatesessionfactory.getsession ();
Tx=session.begintransaction ();
Queries all fields of the entire object
String hql= "from User";
Query query=session.createquery (HQL);
List<user> list=query.list ();
Iterator Its=list.iterator ();
while (Its.hasnext ()) {
user user= (user) Its.next ();
System.out.println (User.getid () + "" +user.getusername () + "" +user.getuserpass ());
}
Console
1 Jordan 123
2 Rodman 123
2) Get a single property
public static void Main (string[] args) {
//TODO auto-generated A stub session
Session=null;
Transaction Tx=null;
Session=hibernatesessionfactory.getsession ();
Tx=session.begintransaction ();
Queries all fields of the entire object
String hql= "SELECT id from User";
Query query=session.createquery (HQL);
List<java.lang.integer> list=query.list ();
Iterator Its=list.iterator ();
while (Its.hasnext ()) {
int a= (Java.lang.Integer) Its.next ();
System.out.println (a);
}
Console:
1
2
3 Query multiple properties when querying multiple properties, the returned list collection element type is a object[] array (that is, an array of objects), where the collection element is an array of objects, not the collection as an array of objects.
public static void Main (string[] args) {
//TODO auto-generated A stub session
Session=null;
Transaction Tx=null;
Session=hibernatesessionfactory.getsession ();
Tx=session.begintransaction ();
Queries all fields of the entire object
String hql= "Select Username,userpass from User";
Query query=session.createquery (HQL);
List<object[]> list=query.list ();
Iterator Traversal iterator its=list.iterator ();
while (Its.hasnext ()) {
object[] a= (object[)) Its.next ();
System.out.println (a[0].tostring () + "" +a[1].tostring ());
}
}
4) Condition Inquiry
String hql= "from User where username=?" and userpass=? ";
Query query=session.createquery (HQL);
The first way of
//query.setstring (0, "Jordan");
Query.setstring (1, "123");
The second way of
query.setparameter (0, "Jordan", hibernate.string);
Query.setparameter (1, "123", hibernate.string);
List<user> list=query.list ();
for (User user:list) {
System.out.println (User.getid ());
}
Query user name: Jordan, the password is 123 members.
Two ways, one is the setstring way, the other is the Setparameter way.
Output through enhanced for loop
Console:
1
And of course, the conditional query method of the custom index
public static void Main (string[] args) {
//TODO auto-generated A stub session
Session=null;
Transaction Tx=null;
Session=hibernatesessionfactory.getsession ();
Tx=session.begintransaction ();
Queries all fields of the entire object
String hql1= "from User where Username=:username and Userpass=:userpass";
Query query1=session.createquery (HQL1);
The first way of
/*query.setstring ("username", "Jordan");
* Query.setstring ("Userpass", "123")
* *
*//The second general
, the third parameter determines the type
query1.setparameter ("username", "Jordan", hibernate.string);
Query1.setparameter ("Userpass", "123", hibernate.string);
List<user> list1=query1.list ();
for (User user:list1) {
System.out.println (User.getid ());
}
}
Console:
1
Query way There are many, mostly in this extension of the above, compared to the criteria query, query way more flexible, more efficient.