1. Download Hibernate jar Package: hibernate-release-4.3.8.final ORM, import the necessary jar package with the path: hibernate-release-4.3.8.final\lib\ Required There are 10 jar packages included.
2. Create a new Java project.
3. Learn to build your user Library:
(a) Project right-click--build path--configure build Path--add library.
(b) Select User-library, in which a new library is named Hibernate.
(c) The jar package required to add hibernate to the Library (path: hibernate-release-4.3.8.final\lib\required), Hello World is enough, and the others are added.
4. Introduce the JDBC driver for the database. I used the Mysql:mysql-connector-java-5.1.7-bin.jar.
(a) Creation of databases: Create DATABASE Wkh;
(b) Switch database: Use Wkh;
(c) Create an employee table:
CREATE TABLE EMPLOYEE(ID INT not NULL auto_increment,first_name VARCHAR( -) defaultNULL,last_name VARCHAR( -) defaultNULL,Salary INTdefaultNULL,PRIMARY KEY(ID));
5. Create hibernate configuration file in src directory hibernate.cfg.xml
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-configuration SYSTEM "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >< hibernate-configuration> <session-factory> <property name= "Hibernate.dialect" > Org.hibernate.diale Ct. Mysqldialect </property> <property name= "Hibernate.connection.driver_class" > Com.mysql.jdbc.Driver </property> <!--Assume test is the database name--<property name= "Hibernate.connection.url" > Jdbc:mysql://localhost:3306/wkh </property> <property name= "Hibernate.connection.username" > Root </property> <property name= "Hibernate.connection.password" > Root </property> <!--List of XML mapping Files---<mapping resource= "Employee.hbm.xml"/></session-factory></ Hibernate-configuration>
5, the establishment of employee class,Employee.java
public class Employee { private int id; Private String firstName; Private String lastName; private int salary; Public employee () {} public employee (string fname, string lname, int salary) { this.firstname = fname; This.lastname = lname; This.salary = salary; } public int getId () { return ID; } public void setId (int id) { this.id = ID; } Public String Getfirstname () { return firstName; } public void Setfirstname (String first_name) { this.firstname = first_name; } Public String Getlastname () { return lastName; } public void Setlastname (String last_name) { this.lastname = last_name; } public int getsalary () { return salary; } public void setsalary (int salary) { this.salary = salary; }}
6, the SRC directory to establish the mapping file Employee.hbm.xml
<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping dtd//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
7, set up test class Testemployee.java
Import java.util.List; Import Java.util.date;import java.util.Iterator; Import org.hibernate.HibernateException; Import org.hibernate.Session; Import Org.hibernate.transaction;import Org.hibernate.sessionfactory;import org.hibernate.cfg.Configuration; public class Manageemployee {private static sessionfactory factory; public static void Main (string[] args) {try{factory = new Configuration (). Configure (). Buildsessionfactory () ; }catch (Throwable ex) {System.err.println ("Failed to create Sessionfactory object." + ex); throw new Exceptionininitializererror (ex); } manageemployee ME = new Manageemployee (); /* ADD few employee records in database */Integer empID1 = Me.addemployee ("Zara", "Ali", 1000); Integer empID2 = Me.addemployee ("Daisy", "Das", 5000); Integer empID3 = Me.addemployee ("John", "Paul", 10000); /* List down all the employees */me.listemployees (); /* Update Employee ' s records */ME.updateemployee (empID1, 5000); /* Delete an employee from the database */Me.deleteemployee (empID2); /* List down new list of the employees */me.listemployees (); }/* Method to CREATE a employee in the database */public Integer AddEmployee (String fname, string lname, int salary ) {Session session = Factory.opensession (); Transaction tx = NULL; Integer EmployeeID = null; try{tx = Session.begintransaction (); Employee Employee = new Employee (fname, lname, salary); EmployeeID = (Integer) session.save (employee); Tx.commit (); }catch (hibernateexception e) {if (tx!=null) Tx.rollback (); E.printstacktrace (); }finally {session.close (); } return EmployeeID; }/* Method to READ all the employees */public void Listemployees () {Session session = Factory.opensession (); Transaction tx = NULL; try{tx = Session.begintransaction (); LisT employees = Session.createquery ("from Employee"). List (); for (Iterator Iterator = Employees.iterator (); Iterator.hasnext ();) {Employee employ EE = (Employee) iterator.next (); System.out.print ("First Name:" + employee.getfirstname ()); System.out.print ("Last Name:" + employee.getlastname ()); System.out.println ("Salary:" + employee.getsalary ()); } tx.commit (); }catch (hibernateexception e) {if (tx!=null) Tx.rollback (); E.printstacktrace (); }finally {session.close (); }/* Method to UPDATE salary for a employee */public void UpdateEmployee (Integer EmployeeID, int salary) { Session session = Factory.opensession (); Transaction tx = NULL; try{tx = Session.begintransaction (); Employee employee = (employee) session.get (Employee.class, EmployeeID); Employee.setsalary (Salary); SEssion.update (employee); Tx.commit (); }catch (hibernateexception e) {if (tx!=null) Tx.rollback (); E.printstacktrace (); }finally {session.close (); }/* Method to DELETE a employee from the records */public void Deleteemployee (Integer EmployeeID) {Session Session = Factory.opensession (); Transaction tx = NULL; try{tx = Session.begintransaction (); Employee employee = (employee) session.get (Employee.class, EmployeeID); Session.delete (employee); Tx.commit (); }catch (hibernateexception e) {if (tx!=null) Tx.rollback (); E.printstacktrace (); }finally {session.close (); } }}
8. Test and Operation success
Manually using hibernate to connect to a database under Eclipse