The first step: Download the Hibernate development package:
Http://sourceforge.net/projects/hibernate/files/hibernate3
Step Two: Hibernate framework directory structure:
Documentation:hibernate Documentation
Lib:hibernate Development Jar Package
Bytecode: Operation byte code jar package.
Jpa:hibernate implementation of the JPA specification.
Optional jar package for optional:hibernate.
The required:hibernate must be a jar package.
Engineering provided by Project:hibernate
Step three: Create a project: (Java Engineering)
Import the appropriate jar package:
Hibernate3.jar
Hibernate_home/lib/required/*.jar
Hibernate_home/lib/jpa/hibernate-jpa-2.0-api-1.0.1.final.jar
To import a log record package:
Log4j-1.2.16.jar
Slf4j-log4j12-1.7.2.jar
Import Database driver:
Mysql-connector-java-5.0.4-bin.jar
Fourth step: Create a table: (relational database)
Create Database hibernate3_day01; Use hibernate3_day01; Create Table intprimarykey varchar ( int);
Fifth step: Create an Entity class: (Object-oriented)
Public classCustomer {Private intID; PrivateString name; Private intAge ; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
Sixth step: Create an ORM Mapping
The mapping file can be as long as it is an XML format file. The name is arbitrary. Typically, the name specification: the entity class name. Hbm.xml
<?XML version= "1.0" encoding= "UTF-8"?><!--Introducing Constraints -<!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "> <hibernate-mapping> <!--To establish a class-to-table mapping - <!--Class tag: Used to map the relationship between a class and a table name: Full path to the class table: tables name - <classname= "Cn.yzu.hibernate3.demo1.Customer"Table= "Customer"> <!--to create a field mapping from a property in a class to a table - <!--uniquely identifies - <!--Configure unique properties with the label of the ID - <!--Configure a primary key generation strategy in the <id> tab. - <IDname= "id"column= "id"> <Generatorclass= "Native"/> </ID> <!--Normal Properties - <!--Property Tags: normal properties in a map class name: attribute names in a class, column: Field names in a table - <!--type: Three notation * java type: java.lang.String * Hibernate type: String * SQL Type: Cannot use the type attribute directly, need sub-label <column> * <column name= "name" sql-type= "varchar (20 ) "/> - < Propertyname= "Name"column= "Name"type= "string"/> < Propertyname= "Age"column= "Age"/> </class></hibernate-mapping>
Seventh step: Create a Hibernate core configuration file
Notifies hibernate which database the connection is. Create a hibernate.cfg.xml under SRC
<?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> <!--properties that have to be configured - <!--to configure basic information for a database connection: - < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Hibernate.connection.url">jdbc:mysql:///hibernate3_day01</ Property> < Propertyname= "Hibernate.connection.username">Root</ Property> < Propertyname= "Hibernate.connection.password">123</ Property> <!--the dialect of Hibernate - <!--generate the underlying SQL different - < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</ Property> <!--Optional Properties - <!--Show SQL - < Propertyname= "Hibernate.show_sql">True</ Property> <!--Formatting SQL - < Propertyname= "Hibernate.format_sql">True</ Property> <!--hbm: Map to ddl:create drop alter - < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property> <!--notifies hibernate to load those mapping files - <MappingResource= "Cn/yzu/hibernate3/demo1/customer.hbm.xml" /></session-factory></hibernate-configuration>
Eighth step: Write the test
@Test//inserting a record into the database Public voiddemo1 () {//1.Hiberante Framework Load Core configuration file (with database connection information)Configuration Configuration =NewConfiguration (). Configure (); //2. Create a sessionfactory. (Get session--quite connected object)Sessionfactory sessionfactory =configuration.buildsessionfactory (); //3. Get the Session object.Session session =sessionfactory.opensession (); //4. By default, transactions are not automatically committed.Transaction tx =session.begintransaction (); //5. Business logic Operations//insert a record into the database:Customer customer =NewCustomer (); Customer.setname ("Ren Tong"); Customer.setage (28); Session.save (customer); //6. Transaction Submission Tx.commit (); //7. Releasing Resources Session.close (); Sessionfactory.close ();}
Hibernate Quick Start