I. Overview: The hibernate framework acts on the DAO layer, enabling the persistence of the data. Manipulate the database in an object-oriented manner.
Two. Hibernate frame construction
1. Guide Package
All jar packages under the required folder in the Lib directory.
MySQL driver package.
2. Create a database in a table.
3. Create the entity class.
4. Create an Entity mapping file (as an example of CRM practice customer Class)
The entity class name. hbm.xml
Introducing constraint Files
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "><!--root element package (optional): Fill in the pack name. The name of the package can be omitted in the following areas where the full class name is required . -<hibernate-mapping Package= "Cn.itcast.domain" > <!--class: Mapping the relationship between a class and a table Name property: Entity Property Name table property: Corresponding table name - <classname= "Customer"Table= "Cst_customer" > <!--ID: Map primary KEY Attribute name (OID) corresponds to primary key column Name property: OID Name column property (optional): Primary key columns name, default value is Name property value length property (optional): Specifies The length of the property. Default value use database corresponding column length type property (optional): Specifies the type of the current column (property). The default value automatically specifies the type based on the database type. Type= "Long" hibernate type type= "Java.lang.Long" Java type <column name= "Cust _id "sql-type=" bigint "></column> database type - <IDname= "cust_id" > <!--primary key generation policy increment:hibernate each time the data is saved, it queries the largest value in the database and adds 1 to the maximum value as the new primary key value (used for testing) Identity: primary key self-increment, database is responsible for generating primary key value Sequence: Sequence, Oracle using Hilo: High-low-bit algorithm for libraries that do not support either self-increment or sequence (no need) Native:identity|sequence|hilo automatic three-choice UUID: The primary key type is used for strings. Assigned: We have manually specified the ID value - <Generatorclass= "Native"></Generator> </ID> <!--Property : Mapping of non-primary key attribute names to non-primary key columns The Name property: Attribute Name column property (optional): The default value is the Name property value the length attribute (optional): Specifies the property length. Default values use database corresponding column length type property (optional): Specifies the type of the current column (property). The default value automatically specifies the type based on the database type. Type= "Long" hibernate type type= "Java.lang.Long" Java type <column name= "Cust _id "sql-type=" bigint "></column> database type - < Propertyname= "Cust_name"column= "Cust_name" ></ Property> < Propertyname= "Cust_source" ></ Property> < Propertyname= "Cust_industry"column= "Cust_industry" ></ Property> < Propertyname= "Cust_level"column= "Cust_level" ></ Property> < Propertyname= "Cust_phone"column= "Cust_phone" ></ Property> < Propertyname= "Cust_mobile"column= "Cust_mobile" ></ Property> </class></hibernate-mapping>
Creating a master configuration file
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 "> <!--root element - <hibernate-configuration> <!--The following are all configured for the Sessionfactory object - <session-factory> <!--Required configuration//dialect//The SQL statements for all databases are based on the SQL99 standard//each database adheres to the SQL99 standard, and a subset of SQL statements are also expanded. Beyond these standards SQL statement is called dialect MySQL dialect: limit 0,5//Note: MySQL dialect Class A total of 3. Be sure to choose the shortest #hibernate. Dialect org.hibernate.dialect.m Ysqldialect//Database driver #hibernate. Connection.driver_class com.mysql.jdbc.Driver//Database connection URL #hibernate. Connection.url jdbc:mysql:///test//Connect user name #hibernate. connection.username Gavin Connection password #hibernate. Connection.password - < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Hibernate.connection.url">jdbc:mysql:///hibernate_54</ Property> < Propertyname= "Hibernate.connection.username">Root</ Property> < Propertyname= "Hibernate.connection.password">1234</ Property> < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</ Property> <!--Optional Configuration//whether hibernate-generated SQL is displayed in the console Hibernate.show_sql true//whether the SQL statement displayed to the console is formatted Hibernate.format_sql true//auto-Build Table # Create (used when testing): Automatically build the table, each time you start hibernate, the table will be automatically created. # Create-drop (used during testing): Automatically builds a table, and automatically builds the table each time hibernate is started. When you release a resource, all tables are deleted. # Update (Common): The table is automatically built, and the table is no longer created if the table that already exists does not exactly match. The table structure is automatically modified. # Validate: Check the table structure. The table is not automatically built. Every time hibernate starts, the table structure is checked for correctness. Incorrect = throws an exception. - < Propertyname= "Hibernate.show_sql">True</ Property> < Propertyname= "Hibernate.format_sql">True</ Property> < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property> <!--mapping introduces Configuration resource properties: Fill in the path to the map file. Relative to the SRC directory. - <MappingResource= "Cn/itcast/domain/customer.hbm.xml" /> </session-factory> </hibernate-configuration>
Hibernate frame Environment Construction