134 (Experimental Class) Lucky Nan Lin
Learn hibernate to know the connection database can be so easy, Java DAO really is cow a force Ah!
Query Summary:
1. Use the HQL statement query q = session.createquery ("Select E from Com.sun.demo.Emp e");
2. Use the Load method (primary key query) Emp e = (EMP) session.load (Emp.class, 1141);
3. Use the Get method (primary key query) Emp e = (EMP) session.get (Emp.class, 1141);
4. Parameterized queries (using the? wildcard character, or command wildcard) Query q = session.createquery ("Update Userinfo set ename= ' AAA ' WHERE ename=?"); Q.setparameter (0, "SMITH");
Query q = session.createquery ("Update Userinfo set ename= ' AAA ' WHERE ename like?");
Q.setparameter (0, "%M%");
Query q = session.createquery ("Update Userinfo set ename= ' AAA ' WHERE ename like:lkename");
Q.setparameter ("Lkename", "%l%");
5. Name the query (put the query statements together to increase the maintainability of the system) add the HQL statement to the *.hbm.xml as follows, and note that it is placed outside the class label <query name= "Myquery" > <! [cdata[from com.sun.hibernate.Employer where job =? ]]> </query>
Query q = session.getnamedquery ("Myquery"); Q.setparameter (0, "MANAGER");
6. Query Query q = session.createquery ("Select Max (SAL) from employer e where Sal was not null"); Query q = session.createquery ("SELECT distinct job from employer e");
7. Instantiate the query steps as follows:
(1). Write your HQL statement
(2). Create a normal Java class-------------------unlike the Pojo class, it has no relation to the database
(3). In the Java class you need to create and query the fields corresponding to the results
(4). In the Java class, you need to create the appropriate constructor
(5). Refine your hql statement and wrap it in a way that instantiates queries
(6). The result obtained by List.get (i) is no longer an array, but a wrapped object
Finally, a piece of code as ending.
Import org.hibernate.Transaction;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Session;
Import org.hibernate.cfg.Configuration;
Import java.util.List;
Import Org.hibernate.Query;
Import Java.util.Iterator;
public class Hbmtestdao {
private static sessionfactory sessionfactory = null;
Private session session = NULL;
Transaction Transaction = null;
static{
try{
Sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
}catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Query method
* @return
*/
Public List SELECTTB () {
List List = null;
Session = Sessionfactory.opensession ();
Transaction = Session.begintransaction ();
String hql = "from Test";
try{
Query query = session.createquery (HQL);
List = Query.list ();
}catch (Exception e) {
E.printstacktrace ();
}
Transaction.commit ();
Session.close ();
return list;
}
/**
* Fuzzy query;
*/
Public List selectbyname (String name) {
List it = null;
String hql = "Select User from Test as user where User.Name like:name";
Session = Sessionfactory.opensession ();
Transaction = Session.begintransaction ();
Query query = session.createquery (HQL);
Query.setstring ("name", "%" +name+ "%");
it = Query.list ();
Transaction.commit ();
Session.close ();
return it;
}
/**
* Search by ID
*/
Public List Selbyid (Integer ID) {
List List = null;
String hql = "from Test where id =" +ID;
Session = Sessionfactory.opensession ();
Query query = session.createquery (HQL);
Query.setinteger ("id", Id.intvalue ());
List = Query.list ();
Session.begintransaction (). commit ();
Session.close ();
return list;
}
/**
* Insertion method;
*/
public int INSERTTB (test test) {
int i=0;
Session = Sessionfactory.opensession ();
try{
Session.begintransaction ();
Session.save (test);
Session.begintransaction (). commit ();
I=1;
}catch (Exception e) {
E.printstacktrace ();
}
Session.close ();
return i;
}
/**
* Delete method;
*/
public int DELTB (Integer ID) {
int i = 0;
Session = Sessionfactory.opensession ();
try{
Session.begintransaction ();
Test test = (test) session.get (Test.class,id);
Session.delete (test);
Session.begintransaction (). commit ();
Session.close ();
i = 1;
}catch (Exception e) {
E.printstacktrace ();
}
return i;
}
/**
* Update method;
*/
public int UPTB (test test) {
int i = 0;
Session = Sessionfactory.opensession ();
try{
Session.begintransaction ();
Session.update (test);
Session.begintransaction (). commit ();
Session.close ();
i = 1;
}catch (Exception e) {
E.printstacktrace ();
}
return i;
}
}
Step-by-step hibernate