This paper gives an example of how to build and configure hibernate environment. Share to everyone for your reference, specific as follows:
1. Download Hibernate jar Package: hibernate-release-4.3.5.final, import the necessary jar package, the path is: hibernate-release-4.3.5.final\lib\required.
There are 10 jar packages included.
2. Create a new Java project.
3. Learn to build your own user Library:
(a) The project right key--build path--configure build Path--add library.
(b) Select User-library to create a new library in which to name hibernate.
(c) The jar package (path: hibernate-release-4.3.5.final\lib\required) required to add hibernate to the library, Hello World is enough, and the others are added.
4. JDBC driver that introduces the database. I used the Mysql:mysql-connector-java-5.1.7-bin.jar.
(a) Create a database:
Create DATABASE hibernate;
(b) Switching the database:
(c) Creation of student tables:
CREATE TABLE Student (ID int primary key,name varchar (), age int);
5. The establishment of Hibernate configuration file Hibernate.cfg.xml, strongly recommended in Hibernate-release-4.3.5.final\documentation\manual\en-us\html_ Copy in the Help document under single path.
Location: 1.1.4. Hibernate configuration. After content modification:
<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/d Td/hibernate-configuration-3.0.dtd ">
Establish Student class:
public class Student {
private int id;
private String name;
private int age;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
}
To create a Student mapping file: Student.hbm.xml
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">
Final Test:
Import org.hibernate.Session;
Import org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import com.huxing.hibernate.model.Student;
public class Studenttest {public
static void Main (string[] args) {
Student a = new Student ();
A.setid (123);
A.setage (a);
A.setname ("Hello hibernate!");
Configuration cfg = new Configuration ();
Sessionfactory CF = Cfg.configure (). Buildsessionfactory ();
Session session = Cf.opensession ();
Session.begintransaction ();
Session.save (a);
Session.gettransaction (). commit ();
Session.close ();
Cf.close ();
}
I hope this article will help you to hibernate framework program design.