Easy to get started with the SSH-hibernate01 environment for beginners to build,
Here we will continue to learn about another framework in the SSH framework-hibernate. So 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.
First, we will build a hibernate environment.
Step 1: Install the jboss tools plug-in. It can be divided into online installation and offline installation. First Install it online: eclipse-> Help-> Eclipse Marketplace-> Search for hibernate-> Find Jboss Tools-> click Install. When this appears, check out other Tools. The installation process requires network connection. Wait until the installation is complete.
For offline installation, check http://www.jboss.org/tools/download. Download it to your local computer or go to eclipse> Help> Install new software> add. click Archive to download the plug-in to your local device. Otherwise, enter the plug-in lication and click OK.
Step 2: in the previous chapter, we have learned how to build the maven environment and create a project. Then we start to use the maven project to use hibenate. Create a maven project in pom. xml introduces javax. servlet 4.0.0, mysql 5.143, hibernate 5.2.10.Final dependency, and change junit version to 4. 12. how to introduce dependencies is described in the previous chapter.
Step 3: Open the mysql database and create a new student table. The table structure is as follows:
Step 3: In the newly created Project, src/main/java creates a package called com. entity and creates a class Student. The class attributes are consistent with those of the database and are encapsulated and constructed.
Step 4: Create a Hibernate XML Mapping file (hbm. xml) under com. entity. remove the default com. entity, add Class to add student Class, and click finsh. Then you will find an additional Student. hbm. xml file in the folder. Open it and take a look. In fact, this will generate a ing file corresponding to your database.
Step 5: Create a New Hibernate Configuration File (cfg. xml) under the src \ main \ resources folder. The default name is the name. Next, you need to configure jdbc based on the database you use. My finsh is fine. This is the configuration file of hibernate.
Step 6: Open cfg. xml and you will find that you have just configured a database connection. You need to add the <mapping resource = "com/entity/Student. hbm. xml"/> node to the <session-factory> </session-factory> node. Resource is the path of the hbm. xml file. You must change. To/hold down the ctrl key, and click the path to jump to hbm. xml. You can use this link ing to find the hbm. xml file, which corresponds to the database. Finally, run Maven test to check whether the project is faulty.
This chapter ends!