0:hibernate Development Steps:
Development steps
1) Setting up a good environment
Introducing Hibernate's Smallest jar package
Preparing the Hibernate.cfg.xml boot configuration file
2) Realistic body type (Pojo)
3) Write the mapping file "User.hbm.xml" for the entity class
Adding mapped entities in Hibernate.cfg.xml
4) Create a library table
5) Write Test class
Get Configuration
Create Sessionfactory
Open session
Open transaction
Use session to manipulate data
Commit a transaction
Close Resource
1:hibernate.cfg.xml configuration file
<?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" >
<session-factory>
The name of the <!--property can be found in Hibernate release package project\etc\hibernate.properties. The hibernate prefix of the parameter name can be omitted--
<!--database Connection configuration--
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://localhost:3306/hibernate</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >root</property>
<!--database Connection pool configuration: Use hibernate built-in data source--
<property name= "Connection.pool_size" >10</property>
<!--Database dialect---
<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property>
<!--level Two cache configuration: temporarily off--
<property name= "Cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>
<!--console print hibernate executed SQL statement--
<property name= "Show_sql" >true</property>
<!--display SQL statements in a readable format--
<property name= "Hibernate.format_sql" >false</property>
<!--automatically generate Ddl:data definition language define table structure, etc.--
<property name= "Hbm2ddl.auto" >update</property>
<mapping resource= "Com/anrongtec/domain/customer.hbm.xml"/>
</session-factory>
2:customer.hbm.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<!--DTD file on: HIBERNATE3.JAR\ORG\HIBERNATE\HIBERNATE-MAPPING-3.0.DTD-
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "Com.anrongtec.domain.Customer" table= "CUSTOMERS" >
<!--map the primary key--
<id name= "id" column= "id" >
<!--PRIMARY key generation strategy: For the moment, remember to use native--
<generator class= "native" ></generator>
</id>
<!--the relationship between attributes in a map class and database table fields--
<property name= "name" column= "name" type= "string" length= "></property>"
<property name= "Gender" column= "Gender" Type= "Boolean" ></property>
<property name= "Birthday" column= "Birthday" ></property>
</class>
3: Test class.
Package com.anrongtec.test;
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;
Import Com.anrongtec.domain.Customer;
public class Customertest {
How the configuration file is loaded.
@Test
public void Savecustomer () {
Customer CS = new Customer (01, "Cao Shaoyang", True, New Date ());
Configuration cfg = new configuration ();
Cfg.configure ();
Sessionfactory sessionfactory = Cfg.buildsessionfactory ();
Session opensession = Sessionfactory.opensession ();
Transaction BeginTransaction = Opensession.begintransaction ();
Opensession.save (CS);
Begintransaction.commit ();
Opensession.close ();
Sessionfactory.close ();
Insert into CUSTOMERS (NAME, GENDER, BIRTHDAY) VALUES (?,?,?)
}
Configuration load config file,
public void Savetest () {
Create an Object
Customer CS = new Customer ("Caoxiaoyang", True, New Date ());
Customer C = new Customer ("Caoxiaoyang", False, New Date ());
Initializing Hibernate's configuration file
Configuration cfg = new configuration ();
Cfg.configure ();//load Hibernate.cfg.xml configuration file
Create a sessionfactory factory
Sessionfactory sessionfactory = Cfg.buildsessionfactory ();
Get Session object: Core. Hibernate is based on the session
Session session = Sessionfactory.opensession ();
Open transaction
Transaction tx = Session.begintransaction ();//Start Transaction
Session.save (CS);//Save Entity Object
Tx.commit ();//Commit a transaction
Releasing the resources that are occupied
Session.close ();
Sessionfactory.close ();
}
public void Save10 () {
Configuration cfg = new configuration ();
Cfg.configure ("Hibernate.cfg.xml");//load Hibernate.cfg.xml configuration file
}
}
4:hibernate Introduction:
Hibernate is an open source framework, which is a framework for object-relational mapping, and it is a lightweight encapsulation of JDBC, and our Java programmers can manipulate the database using object-oriented thinking.
Hibernate Core Interface
Session: Responsible for persistent object CRUD operations
Sessionfactory: Responsible for initializing hibernate, creating Session Object
Configuration: Responsible for configuring and starting hibernate, creating sessionfactory
Transaction: Responsible for things-related operations
Query and Criteria interface: responsible for executing various database queries
Getting Started with hibernate