What is an ORM?
ORM:Object Relational Mapping
O: Object in the object-oriented field (JavaBean object)R: Relational in the relational database field (table structure)
M: Mapping (XML configuration file)
Overview of the Hibernate Framework
Hibernate is an open source object-relational mapping (ORM) framework, which encapsulates JDBC with very lightweight objects, allowing Java programmers to use object programming thinking to manipulate the database at will.
Hibernate can be used in any occasion using JDBC, either in Java client programs or in Servlet/JSP web applications.
Hibernate is a persistent layer solution for lightweight JavaEE applications and a relational database ORM framework.
Build the development environment of Hibernate
download the corresponding jar package
Step 2: Create a table structure
Step 3: Build a Hibernate development environment
MySQL driver jar package The jar package log jar package
required for Hibernate development
public class Customer {
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
// 省略get和set方法
}
Step 5: Create the mapping between the class and the table structure
1. Create the mapping configuration file under the package where the JavaBean is located. The
default naming rule is: entity class name.hbm.xml
Introduce constraints in the xml configuration file
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
2. If you cannot access the Internet, there is no prompt for writing a configuration file , you need to configure it yourself
first copy http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd --> window --> preferences --> search xml --> select xml catalog --> click add --> Now URI --> Paste the copied address --> Select location, select the path of the local DTD
3. Write the mapping configuration file
1. In the src directory, create a configuration file named hibernate.cfg.xml
2. Introduce DTD constraints in XML
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
3. 4 parameters that must be configured
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
The dialect of the database (must be configured)
#hibernate.dialect org.hibernate.dialect.MySQLDialect
The specific configuration is as follows:
Step 7: Write Hibernate entry code
1. The specific code is as follows
/**
* Test save customer
*/
@Test
public void testSave(){
// First load the configuration file
Configuration config = new Configuration();
// load src by default The configuration file in the directory
config.configure();
// Create SessionFactory object
SessionFactory factory = config.buildSessionFactory();
// Create session object
Session session = factory.openSession();
// Open transaction
Transaction tr = session.beginTransaction( );
// write save code
Customer c = new Customer();
// c.setCust_id(cust_id);
c.setCust_name("Test Name");
c.setCust_mobile("110");
// save client
session.save(c);
// commit transaction
tr.commit();
// release resources
session.close();
factory.close();
}