Hibernate Framework Description:
ORM Concept (very important)
The O,object object. r,relation relationship. m,mapping Mapping
ORM: Object Relational Mapping!
ORM, solve what problem?
Storage: Can the object's data be saved directly into the database
Get: Can get an object directly from the database
To achieve the above two points, there must be a mapping
Summary: Relationship between Hibernate and Orm: Hibernate is the implementation of ORM
How to build a hibernate development environment, development steps:
(1) Download Source: Version hibernate-distribution-3.6.0.final
(2) Introduction of JAR file: Hibernate3.jar core +required must be introduced (6) +jsp directory +mysql Driver
(3) Mapping of objects and objects
Employee.java Object
Mapping of Employee.hbm.xml objects (typically we use the hbm.xml end, which is to differentiate the Hibernate mapping file)
(4) Src/hibernate.cfg.xml (the name of this file must be written in this way)
1.employee.java
Package Cn.itcast.hello;import Java.util.date;public class Employee {private int id;private String empname;private Date W orkdate;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getempname () {return empname;} public void Setempname (String empname) {this.empname = EmpName;} Public Date Getworkdate () {return workdate;} public void Setworkdate (Date workdate) {this.workdate = workdate;}}
2.employee.hbm.xml file
Primarily includes primary key mappings and non-primary key mappings
<?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 ">
3.hibernate.cfg.xml file(1) Configuration of the database connection. If you do not know can go to hibernate source/project/etc/hibernate.properties find copy paste can
(2) Other configurations include dialect and configuration information that is not automatically constructed
(3) Introduction of the mapping file. <mapping resource= "Directory of configuration Files"/>
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibern Ate.org/dtd/hibernate-configuration-3.0.dtd ">4. Test class ApptestTo write the steps:
(1). Gets the management class object that loads the configuration file. (2). Load the configuration file. (3). Create a session factory object. (4). Create a session (which represents a conversation that is connected to a database). (5). Turn on the transaction. (6). Operation with the database . (7). Commit the transaction. (8). Close the resource.
Package Cn.itcast.hello;import Java.util.date;import Org.hibernate.sessionfactory;import org.hibernate.Transaction ; Import Org.hibernate.cfg.configuration;import Org.hibernate.classic.session;import Org.junit.test;public class apptest {@Testpublic void Test () {Employee emp=new employee (); Emp.setempname ("Li Weikang"); Emp.setworkdate (new Date ());//1. Gets the management class object that loads the profile configuration config=new configuration ();//2. Load configuration file Config.configure ();// The Hiberate.xml file//3 is loaded by default in the SRC directory. Create session factory object Sessionfactory factory = Config.buildsessionfactory ();//4. Create Session ( Represents a conversation, a session with a database connection) session session = Factory.opensession ();//5. Open transaction Transaction tx = Session.begintransaction ();//6. Operation with Database Session.save (EMP);//7. Commit Transaction Tx.commit ();//8. Close Resource Session.close ();}}
Running results can be found in the database with an employee table, and a record
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hibernate Getting Started case