A:. Project structure
Second, the Code
1) hibernateutil
Packageutil;Importorg.hibernate.SessionFactory;Importorg.hibernate.boot.MetadataSources;ImportOrg.hibernate.boot.registry.StandardServiceRegistry;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder; Public classHibernateutil {Private Staticsessionfactory SF; Static{Standardserviceregistry Registry=Newstandardserviceregistrybuilder (). Configure ()//The experimental method gives a parameter to see if the config file name is not hibernate.cfg.xml. Build (); SF=Newmetadatasources (registry). Buildmetadata (). Buildsessionfactory (); } Public Staticsessionfactory getsessionfactory () {returnSF; }}
2) configuration file Hibernate.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-Configuration Public"-//hibernate/hibernate Configuration DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" & gt;true</property> <property name= "Format_sql" >true</property> <mapping resource= "Entity/studentmapping.xml"/> </session-factory>3.1) Entity Object
Packageentity; Public classStudent {PrivateString Sno; PrivateString sname; PublicString Getsno () {returnSno; } Public voidSetsno (String Sno) { This. Sno =Sno; } PublicString Getsname () {returnsname; } Public voidsetsname (String sname) { This. sname =sname; } }
3.2) Entity mapping file
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-Mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" Package = " Entity > <class name= "Student" table= "Students" > <id name= "sno" column= "id" > class= "Assigned" ></generator> </id> <property name= "Sname" ></property > </class>
4) Dao method
PackageDAO;Importjava.util.List;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importentity. Student;Importutil. Hibernateutil; Public classStudentdao {Privatesessionfactory SF; PublicStudentdao () {SF=hibernateutil.getsessionfactory (); } /*Add*/ Public voidInsert (Student stu) {Session s=sf.opensession (); Transaction TS=s.begintransaction (); /*Add*/S.save (STU); Ts.commit (); S.close (); } /*Modify*/ Public voidudate (Student Stu) {Session s=sf.opensession (); Transaction TS=s.begintransaction (); /*Modify*/S.update (STU); Ts.commit (); S.close (); } /*Delete*/ Public voidDelete (String Sno) {Session session=sf.opensession (); Transaction TX=session.begintransaction (); Student Stu= Session.get (Student.class, Sno); /*Delete*/Session.delete (STU); Tx.commit (); Session.close (); } /*Enquiry*/ PublicList<student>GetAll () {Session s=sf.opensession (); String hql= "From Student"; /*Enquiry*/List<Student> list =s.createquery (HQL). List (); S.close (); returnlist; } }
5) Test Testing
Packagetest;ImportDAO. Studentdao;Importentity. Student; Public classMain { Public Static voidMain (string[] args) {Studentdao dao=NewStudentdao (); /*Add*/Student Stu=NewStudent (); Stu.setsno ("22"); Stu.setsname ("Z22"); Dao.insert (Stu); /*Modify*/ /*Student stu = new Student (); Stu.setsno ("11"); Stu.setsname ("2222222"); Dao.udate (Stu);*/ /*Enquiry*/ /*list<student> List = Dao.getall (); for (Student s:list) {System.out.println (S.getsname ()); }*/ /*Delete*/ /*Dao.delete ("a");*/ }}
* * Above personal packing notes, if wrong or do not understand the place, welcome comments and points * *
Hibernate ORM Framework-continuation of the first chapter: Hibernate additions and deletions (optimization of the first hibernate code)