|
At first review Hibernate, just reviewed, we found all forgotten, and even the environment to build will not equal all over again ah, no way to bite the bullet and had to start all over again.
Hibernate is an excellent ORM framework, object relation mapping object-relational mapping. My understanding is automatically put into operation pojo class of objects in the corresponding operations on the database tables. It simply is to create a pojo class object, then the corresponding database table will insert such an object. Modify, delete, of course, of the. It is to be understood as isolated java database operation and Development.
First, download the Hibernate jar package
You find the necessary Hibernate version http://www.Hibernate.org/ in. I downloaded the latest Hibernate-release-4.3.1.Final.
Second, copy the jar package
Copy in lib / required to package all jar lib folder of the project under
Third, the deployment Hibernate.cfg.xml, corresponding pojo of Student.hbm.xml
New Hibernate.cfg.xml file in the src directory of the project. In the documentation / manual / en-US / html_single / index.html
< ? 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">
< Hibernate-configuration>
< Session-factory>
< -! Database connection settings ->
< Property name = "connection.driver_class"> Oracle.jdbc.driver.OracleDriver < / property>
< Property name = "connection.url"> jdbc: oracle: thin: @ 127.0.0.1: 1521: lubby < / property>
< Property name = "connection.username"> admin < / property>
< Property name = "connection.password"> admin < / property>
< -! SQL dialect ->
< Property name = "dialect"> org.hibernate.dialect.Oracle10gDialect < / property>
< -! Disable the second-level cache ->
< Property name = "cache.provider_class"> org.hibernate.cache.internal.NoCacheProvider < / property>
< -! Echo all executed SQL to stdout ->
< Property name = "show_sql"> true < / property>
< -! Drop and re-create the database schema on startup ->
< -! < Property name = "hbm2ddl.auto"> update < / property> ->
< Mapping resource = "com / lubby / pojo / Student.hbm.xml" />
< / Session-factory>
< / Hibernate-configuration>
The first four labels are driven configuration database address and user name password.
< -! SQL dialect ->
< Property name = "dialect"> org.hibernate.dialect.Oracle10gDialect < / property>
This is the name and version of the configuration database. In the documentation / manual / en-US / html_single / index.html can be found in the corresponding database field corresponding I use the oracle 11g is written above the corresponding
< -! Echo all executed SQL to stdout ->
< Property name = "show_sql"> true < / property>
This is to choose whether to display the sql statement.
< Mapping resource = "com / lubby / pojo / Student.hbm.xml" />
This is very important to tell hibernate configuration file configuration file configured pojo class address
Fourth, create a class Student pojo
package com.lubby.pojo;
public class Student {
private String sid;
private String sname;
private int age;
public Student () {
super ();
// TODO Auto-generated constructor stub
}
public Student (String sid, String sname, int age) {
super ();
this.sid = sid;
this.sname = sname;
this.age = age;
}
public String getSid () {
return sid;
}
public void setSid (String sid) {
this.sid = sid;
}
public String getSname () {
return sname;
}
public void setSname (String sname) {
this.sname = sname;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this.age = age;
}
@Override
public String toString () {
return "Student [sid =" + sid + ", sname =" + sname + ", age =" + age
+ "]";
}
}
Fifth, the configuration Student.hbm.xml
Pojo Each class has a corresponding profile. They are placed inside the package pojo class resides.
< ? 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">
< Hibernate-mapping package = "com.lubby.pojo">
< Class name = "Student" table = "tab_stu">
< Id name = "sid" column = "sid"> < / id>
< Property name = "sname" column = "sname"> < / property>
< Property name = "age" column = "age"> < / property>
< / Class>
< / Hibernate-mapping>
< Class name = "Student" table = "tab_stu"> < / class>
name: the name of the class
table: the name of the corresponding database table
< Id name = "sid" column = "sid"> < / id>
id: the primary key is set pojo class, column is the primary key of the corresponding database name. If you do not write the column, and the default name of the same content.
< Property name = "sname" column = "sname"> < / property>
< Property name = "age" column = "age"> < / property>
property: Setting the general properties
Sixth, call hibernate
package com.lubby.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jboss.weld.logging.messages.EventMessage;
import com.lubby.pojo.Student;
public class StudentTest {
public static void main (String args []) {
Student stu = new Student ();
stu.setSid ( "3");
stu.setAge (25);
stu.setSname ( "Li Huitang");
Configuration cfg = new Configuration ();
SessionFactory sf = cfg.configure () buildSessionFactory ().;
Session session = sf.openSession ();
session.beginTransaction ();
session.save (stu);
session.getTransaction () commit ().;
session.close ();
sf.close ();
}
} |
|
|
|