Crud practice of Hibernate

As a highly encapsulated persistence layer framework, hibernate used to be very powerful. Now, although it is not widely used, for example, our former company mainly does OA, and the application framework is Spring + spring MVC + hibernate.

Compared with mybatis, hibernate is not widely used, but it does not mean that it is useless.

Today, I will talk about the crud of hibernate. This article mainly tells readers what hibernate is, why to use hibernate, the advantages and disadvantages of hibernate, and the basic instance application of hibernate.

1、 What is Hibernate

Hibernate is an open source object relational mapping framework. It encapsulates JDBC with very lightweight objects. It establishes the mapping relationship between POJO and database tables. It is a fully automatic ORM framework. Hibernate can automatically generate and execute SQL statements, so that Java programmers can manipulate the database with object programming thinking at will. Hibernate can be used in any situation where JDBC is used, not only in Java client programs, but also in servlet / JSP Web applications. The most revolutionary thing is that hibernate can replace CMP in the javeee architecture of EJB application and complete the task of data persistence (Baidu's description is quoted here)

2、 Why use Hibernate

Why should we use Hibernate? We don't answer why we should use hibernate first, because a technology must have its application scenarios after its entry into WTO.

So what are the advantages of Hibernate?

(1) With the standard ORM framework, programmers do not need to write SQL statements

(2) It has good database independence, that is, if the database changes, the code does not need to be written again;

Everything has its advantages and disadvantages

So what are the disadvantages of Hibernate?

(1) The learning threshold is high, so you need to have a good foundation for the data relationship model, and you need to consider the trade-off between performance and object model when setting or mapping;

(2) Programmers cannot independently optimize SQL performance;

So what are the application scenarios of Hibernate?

For example, projects with clear requirements and fixed business, such as OA projects, ERP, CRM and other projects

3、 Basic instance of Hibernate

I remember when I first learned hibernate a long time ago, although there were many examples on the Internet, I didn't think they were what I wanted, because they were incomplete and not a special system, but if they were too systematic, they would be serialized. However, I think for beginners, sometimes watching serials is a little sleepy and boring. This example takes Maven project as an example. Maven is one of the most popular project management tools at present.

The following example demonstrates and explains:

1. Import Maven dependency

2. Write the main configuration files of hibernate

hibernate. cfg. xml

Four elements of database: loading driver, establishing connection, user name and password. I won't say more about that.

hibernate. Dialect: the good portability of database dialect hibernate is reflected here. In the face of different databases, you only need to change the dialect

hibernate. show_ SQL: whether to print SQL statements. It is recommended for the development environment. It is not recommended for the production environment

hibernate. hbm2ddl. Auto: it is generally recommended to use update instead of create

current_ session_ context_ Class: This is mainly for session objects, which I will explain later

3. Author entity

User. java

4. Write the mapping file corresponding to the entity

User. hbm. xml

The name attribute in column is mainly used to map the object entity to the table

Type: entity attribute

Length: length

Not null: whether it is empty. The default value is false. It is not empty

Unique unique

5. Packaging tools

HibernateUtils. java

6. Write test class

The following is the specific crud operation. There are some comments. Just remove the comments to test the effect.

Summary:

The code of this article is placed at: https://github.com/youcong1996/study_simple_demo.git

The branch is a hibernate crud branch

If you encounter many problems in reusing my article and cannot solve them, you can directly clone my git warehouse and run it locally

As shown in the figure:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>