Hibernate introduction and entry-level cases, and hibernate entry-level cases

Source: Internet
Author: User

Hibernate introduction and entry-level cases, and hibernate entry-level cases

I fought for three thousand million yuan, and I used to work as a million division.

If you haven't learned the three major SSH frameworks before, you must have used JDBC to operate the database. Now, you can directly submit the database interaction operations to Hibernate.

Hibernate is an open-source object relationship ing framework that encapsulates JDBC very lightweight objects. It establishes ing between POJO and database tables and is a fully automated orm framework, hibernate can automatically generate and execute SQL statements, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.

Hibernate is a persistence layer ORM framework. (ORM: Object Relational Mapping. Object link ing. The development language uses Java, object-oriented (Object ). The databases used are Relational databases ). It is to establish a ing relationship between the object and the table in the database, and the operation object can operate on the table .)

After learning about hibernate, are you looking forward to it? OK. Let's take a start-up Hibernate case.

Step 1: Download The Hibernate Development Kit.: https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/

Step 2: After the download is completed and decompressed, you can see the following directory:

Step 3: Go to eclipse to create a web project and introduce necessary jar packages. The jar packages are in the lib/required directory above.

At the same time, you also need a database driver package and a log package. The jar package of a complete project in an entry case is as follows:

Step 4: Create a database and a table,

Database creation:

Create database hibernate

Table creation:

Create table 'cst _ customer '(

'Cust _ id' bigint (32) not null AUTO_INCREMENT COMMENT 'customer id (primary key )',

'Cust _ name' varchar (32) not null comment 'customer name (company name )',

'Cust _ source' varchar (32) default null comment 'customer information source ',

'Cust _ industry 'varchar (32) default null comment 'customer industry ',

'Cust _ level' varchar (32) default null comment 'customer level ',

'Cust _ phone' varchar (64) default null comment 'landline phone ',

'Cust _ mobile' varchar (16) default null comment 'mobile phone ',

Primary key ('cust _ id ')

) ENGINE = InnoDB AUTO_INCREMENT = 1 default charset = utf8;

Step 5: Create an object class based on the database table and generate the get and set methods

Public ClassCustomer {

PrivateLong cust_id;

PrivateString cust_name;

PrivateString cust_source;

PrivateString cust_industry;

PrivateString cust_level;

PrivateString cust_phone;

PrivateString cust_mobile;

// Generate the corresponding get and set methods

}

Step 6: Create a ing. The ing file usually has a naming rule: Class Name. hbm. xml: The ing file is placed in the same directory as the object class. when creating a ing file, you must first introduce constraints. You do not need to write this code. Copy the code to the location shown in figure:

Open Java Resources/Libraries/Web App Libraries

Turn on hibernate-core-5.0.7.Final.jar/org. hibernate

Find and open hibernate-configuration-3.0.dtd at the bottom

Copy the following section.

The following figure shows the Configuration:

If there is no network connection when writing the configuration file and no prompt is displayed, You need to configure the relevant file:

Copy the last section of the constraint to open the browser, and a dtd file will be automatically downloaded.

In the first option of eclipse, find that key is the last section of the constraint, and location is the path of the dtd file you just downloaded, after the configuration is complete, a prompt will be displayed when no network is available. If not, restart the system.

 

Step 7: Create the core configuration file of Hibernate in the src directory:

<? 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>

<! -- Database connection information -->

<Property name ="Hibernate. connection. driver_class"> Com. mysql. jdbc. Driver </property>

<Property name ="Hibernate. connection. url"> Jdbc: mysql: // hibernate </property>

<! -- Configure the username and password used to connect to the database -->

<Property name ="Hibernate. connection. username"> Root </property>

<Property name ="Hibernate. connection. password"& Gt; 123 </property>

<! -- Database Dialect: Different SQL statements are generated based on the underlying database -->

<Property name ="Hibernate. dialect"> Org. hibernate. dialect. MySQLDialect </property>

<! -- Configure and display SQL -->

<Property name ="Hibernate. show_ SQL"> True </property>

<! -- Configure and format SQL -->

<Property name ="Hibernate. format_ SQL"> True </property>

<! -- Configure hbm2ddl

Values of hibernate. hbm2ddl. auto

* None: Do not convert the ing to DDL.

* Create: If a table exists, delete the original table. A new table is created each time. Used for testing.

* Create-drop: If a table exists, the original table is deleted. A new table is created each time. After the operation is completed, the table will be deleted. Used for testing.

* Update: if there are tables in the database, use the original table. If no table exists, a table is created. In addition, the original table structure can be updated.

* Validate: no table is created. Check whether the ing and table structure are correct

-->

<Property name ="Hibernate. hbm2ddl. auto"> Update </property>

<! -- Load the ing file -->

<Mapping resource ="Com/hibernate/domain/Customer. hbm. xml"/>

</Session-factory>

</Hibernate-configuration>

 

Step 8: Compile the test method

Public ClassHibernateDemo1 {

@ Test

/**

* Save operation

*/

Public VoidDemo1 (){

// Load the core configuration file of Hibernate.

Configuration configuration =NewConfiguration (). configure ();

// Create a SessionFactory object.

SessionFactory sessionFactory = configuration. buildSessionFactory ();

// Create a Session (equivalent to Connection in JDBC)

Session session = sessionFactory. openSession ();

// Start the transaction:

Transaction transaction = session. beginTransaction ();

// Complete the operation:

Customer customer =NewCustomer ();

Customer. setCust_name ("XX ");

// Save Method

Session. save (customer );

// Submit the transaction

Transaction. commit ();

// Release resources

Session. close ();

}

}

Execution method, you can add records in the database .......

So far, you have got started with hibernate. Come on. It will be better in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.