Hibernate Framework of Java Framework

Keywords: Hibernate xml MySQL JDBC

What is a framework?
Answer: Semi-finished projects that can complete some functions

JavaEE Three-tier Architecture
Web
Service
Dao

The 5 framework:
SSH: Spring + Struts2 + Hibernate
SSM: Spring + SpringMVC + MyBatis

hibernate is an orm framework

orm:object relation mapping. Object relation mapping
orm is divided into four levels: dbutils belongs to level 1, mybatis belongs to level 2, hibernate belongs to level 4: fully object-oriented database operation

Construction of hibernate Framework
1. guide pack
2. Create a database and prepare tables to write entity classes
3. Write orm metadata (mapping configuration files between objects and tables)
Import constraints, map configuration files

4. Write the master configuration file

5. Write code.
Configuration of Customer.hbm.xml (ORM metadata)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <!-- beans Entity class names under packages Customer table It's the name of the table.-->
    <class name="beans.Customer" table="cst_customer">
    <!-- Customer Primary key, which must have a primary key, can be omitted when the attribute name is the same as the field name.
    	[type]: Types, field types, can be written in three ways
    	java Type: java.lang.Long
    	Database type: bigint
    	Hibernate Type: long/string/int
    	Automatic recognition without filling in
    	[length]:Set the length without filling in the default maximum
    -->
    <id name="cust_id" column="cust_id">
    <!-- generator:Primary key generation strategy -Tomorrow's Specific Explanation -->
    <generator class="native"></generator>
    </id>
    <!-- name Is the field of the entity class. column Is an attribute of a table -->
    <property name="cust_name" column="cust_name"></property>
    <property name="cust_source" column="cust_source"></property>
    <property name="cust_industry" column="cust_industry"></property>
    <property name="cust_level" column="cust_level"></property>
    <property name="cust_linkman" column="cust_linkman"></property>
    <property name="cust_phone" column="cust_phone"></property>
    <property name="cust_mobile" column="cust_mobile"></property>
    </class>
    </hibernate-mapping>

Hibernate master configuration file src/hibernate.cfg.xml

<?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>
<!-- 
//hibernate Basic Information of Five Compulsory Connection Databases
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password

 -->
 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://192.168.2.10/test</property>
   <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <!-- 
   #hibernate.show_sql true
   		//Display generated sql query statements
   #hibernate.format_sql true
    	//Format sql statement, readable
    -->  
     <property name="hibernate.show_sql">true</property>
     <property name="hibernate.format_sql">true</property>
     <!-- 
     ## auto schema export
     	//Automatic Derived Structure
     #hibernate.hbm2ddl.auto create-drop - Each operation creates a new table and deletes it after the operation.
     #hibernate.hbm2ddl.auto create - Each execution creates a new table and deletes the original table
     					//The above options are not very useful
     #hibernate.hbm2ddl.auto update - There is an operation on the table, there is no creation of a table, and it will be updated if the table structure changes.
     #hibernate.hbm2ddl.auto validate - Used only when tables exist and the structure is consistent
      -->
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!--  Introduce mapping files and use relative path metadata
      		resource: Start with the class path
       -->
      <mapping resource="beans/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

hibernate API Details

Additions and deletions

Posted by pmt2k on Wed, 02 Oct 2019 04:12:34 -0700