SSH online marketplace environment configuration-Analysis of Hibernate
SSH online store although only four days of video, day01-04, but while doing projects while learning, it is really not easy, so far has been for half a month, after two days. It takes about ten days to configure an environment, including adding configuration files and Jar packages. When coding, coupled with a stranger to the development environment, it took a lot of effort, debugging also took a long time, but fortunately, now I am familiar with it and it is easy to do it, the following briefly summarizes the technologies used here.
SSH is an integrated framework developed from three layers. The three layers include the page display layer, business logic layer, and database operation layer. The same applies to SSH, but each layer uses different technologies. In SSH, Hibernate is responsible for data persistence, that is, interaction with the database; Spring is responsible for business logic, and Spring annotation; page display is implemented by Struts2 and Web layer, the MVC Architecture is used to classify pages and logic.
This article briefly introduces Hibernate.
What is Hibernate?
Hibernate is an open-source object relationship ing framework that encapsulates JDBC objects in a lightweight manner, 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.
After creating an online shopping mall, it is easier to understand the definition in a few points. For example:
1. Hibernate can be used in any scenario where JDBC is used. That is to say, Hibernate is related to the database and is used to operate the database.
2. Hibernate belongs to the ORM framework, just like EF used in ITOO. They all belong to the object relationship ing framework. That is to say, Hibernate performs Database Operations just like EF and directly maps to the database.
3. The task of completing data persistence is to emphasize that Hibernate is used for database interaction.
What does Hibernate have?
Hibernate has six core classes and interfaces: Session, SessionFactory, Transaction, Query, Criteria, and Configuration. Hibernate uses these interfaces to achieve Persistent Object Access and transaction control.
1. Session
The Session interface is responsible for executing the CRUD operation on the Persistent Object. CRUD interacts with the database and contains many common SQL statements.
2. SessionFactory
The SessionFactory interface initializes Hibernate, acts as a proxy for data storage sources, and creates Session objects. SessionFactory uses the factory mode. Generally, one SessionFactory is specified for a database, and multiple sessionfactories are specified for multiple database operations. SessionFactory is configured in applicationContext. xml, which is the path for Dao to interact with the database.
3. Transaction
The Transaction interface is optional and an abstraction of the actual Transaction implementation. These implementations include various transactions, so that your project can be transplanted between non-stop environments and containers.
4. Query
The Query interface is used to Query databases and persistent objects. It can be expressed in two ways: HQL and SQL statements of the local database.
5. Criteria
The Criteria interface is similar to the Query interface, allowing you to create and execute object-oriented standardized queries.
6. Configuration
The Configuration class configures and starts Hibernate. During Hibernate startup, the Configuration class instance first locates the Configuration file, reads the content of the Configuration file, and then creates a SessionFactory object.
How to Use Hibernate
1. reference the Jar package
To use Hibernate, You need to reference corresponding Jar packages. These Jar packages can be downloaded online:
Hibernate itself Jar package: hibernate-distribution-3.6.10.Final \ hibernate3.jar, hibernate-distribution-3.6.10.Final \ lib \ required \ *. jar, hibernate-distribution-3.6.10.Final \ lib \ jpa \ *. jar
Slf4j and log4j integration Jar package: slf4j-1.7.2.jar, log4j. jar
Database driver Jar package: mysql-connector-java-5.0.4-bin.jar, here the database is MySQL, So download the MySQL driver Jar package
Connection Pool Jar package: c3p0-0.9.1.jar
2. Configure in the configuration file
The configuration file must be configured in three parts: C3P0 connection pool, Hibernate-related information, and transaction management-related configuration.
C3P0:
Hibernate information:
org.hibernate.dialect.MySQLDialect
true
true
false
update
cn/itcast/shop/user/vo/User.hbm.xml
cn/itcast/shop/category/vo/Category.hbm.xml
cn/itcast/shop/product/vo/Product.hbm.xml
cn/itcast/shop/categorysecond/vo/CategorySecond.hbm.xml
Transaction Management Configuration:
Summary
Hibernate has just been introduced, but it is not very familiar with it. After being configured, it is only a simple understanding. In Hibernate, another Query statement, namely HQL, is used. HQL is The Hibernate Query Language, which is very similar to the Query syntax of SQL statements, provides richer, more flexible, and more powerful query capabilities. The query statements directly transmitted in the online store are written in HQL. With the foundation of SQL, it is not difficult to learn HQL.