Package com. yl. Demo;
Import java. util. iterator;
Import java. util. List;
Import org. hibernate. hibernateexception;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Import com. yl. Bean. classes;
Import com. yl. Bean. student;
Import com. yl. utils. dbutils;
Public class htest {
/**
* @ Param Stu
*/
Public void insert (student Stu ){
// Read the configuration file
Configuration conf = new configuration (). Configure ();
// Obtain the factory
Sessionfactory Sf = Conf. buildsessionfactory ();
// Obtain the session
Session session = NULL;
Transaction Tx = NULL;
Try {
Session = SF. opensession ();
Tx = session. begintransaction ();
Session. Save (Stu );
TX. Commit ();
} Catch (hibernateexception e ){
TX. rollback ();
E. printstacktrace ();
} Finally {
If (session! = NULL & session. isopen ())
Session. Close ();
}
}
Public void findbyid (int id ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student where id =? ");
Query. setinteger (0, ID );
Student Stu = (student) query. uniqueresult ();
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
System. Out. println (STU. getname ());
}
Public void findall01 (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student ");
List <student> List = query. List ();
For (student STU: List ){
System. Out. println (STU. getname ());
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void findall02 (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student ");
Iterator <student> it = query. iterate ();
While (it. hasnext ()){
System. Out. println (it. Next (). getname ());
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void getbyid (int id ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Student Stu = (student) Session. Get (student. Class, ID );
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
System. Out. println (STU. getname ());
}
Public void loadbyid (int id ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Student Stu = (student) Session. Load (student. Class, ID );
// System. Out. println (STU. getname ());
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
System. Out. println (STU. getname ());
}
Public void findsome (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("Select name, age from student ");
List list = query. List ();
For (Object O: List ){
Object [] Ob = (object []) O;
System. Out. println (OB [0] + "" + OB [1]);
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void findlike (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student where name like? ");
Query. setparameter (0, "% C % ");
List <student> List = query. List ();
For (student STU: List ){
System. Out. println (STU. getname ());
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void Update (student Stu ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Session. Update (Stu );
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void insertcia (Classes CIA ){
// Obtain the session
Session session = dbutils. getsession ();
// Start the transaction
Session. begintransaction ();
Session. Save (FIG );
// Transaction commit
Session. gettransaction (). Commit ();
// Close
Dbutils. getclose (session );
}
Public static void main (string [] ARGs ){
Htest ht = new htest ();
Student Stu = new student ();
Stu. setid (1 );
Stu. setname ("aabbcc ");
Stu. setage (20 );
Classes L4 = new classes ();
Linoleic. setclassroom ("306 ");
Linoleic. setname ("Java ");
Stu. setlinoleic (linoleic );
// Ht. insert (Stu );
// Ht. findbyid (1 );
// Ht. findall01 ();
// Ht. findall02 ();
// Ht. getbyid (1 );
// Ht. loadbyid (1 );
// Ht. findsome ();
// Ht. Update (Stu );
Ht. findlike ();
}
}