1. Create a project
Using idea to create a Hibernate database project, you need to have some necessary actions when creating the project, and then step through the instructions below.
First step: Create Idea Project
Note that hibernate is selected here.
Specifies the catalog for the project.
Step Two: Configure the database
Create an instance of MySQL database by using the Database tab in the right column.
Here to fill in the local MySQL information, including the use of the database, here in advance in the MySQL database created a name is test. After completing, click on the Test Connection button on the right side of the center for testing the connection, and normally the connection will be displayed successfully. Such as:
Step three: Create a new package to manipulate the database
To create a new Com.test package:
Fourth step: Import data structure
Call out the configuration of the project according to the persistence button in the bottom right corner
Then right-click Hibernate->generate Persistence maping->by data scheme. In the pop-up box, fill in the following data sheet as required.
Upon completion of the previous step, a hibernateentity Java class can be generated in the Src/com/test package in accordance with the structure of the Hibernate data table in the database in test. This process is automatic.
2. Test the operation of the database
First step: Modify the Hibernate.cfg.xml file
This file is the Hibernate profile for the current project, and you need to add database access information. As shown in the following:
Here is a red warning message because the jar package is not added to MySQL.
You can add a jar package in two ways.
- Manually download the jar package and copy it to the project's Lib library.
- Configure Poem.xml to download dependent jar packages via Maven
Add Poem.xml,poem.xml by using the MAVEN project on the right-hand side of idea to search Baidu by yourself.
Add the following Maven project as follows:
You can look at the external Lib in the project directory on the left, which already has the MySQL library. MySQL's driver in Hibernate.cfg.xml is no longer alarming. But the file here is automatically generated, not complete, we have to refine it.
The following configuration is completed:
Step two: Generate a configuration file for the database entity class
For the current project, the entity class is hibernateentity. A configuration file named HibernateEntity.hbm.xml is generated under the class's sibling directory, specifying the correspondence between the properties in the class and the fields in the data table, as shown in the following example:
Step three: Build the test program and run it
The test program is as follows, just to add a piece of data, you can see that the operation of the data table is completely replaced by the operation of the class.
It is important to note that if there are no available methods at run time, you can see if there are many different versions of Hibernate jar packages in the current project, and if so, delete the others and keep only one version.
By accessing the MySQL database through the Mysql-font tool, you can see that a piece of data has been added:
3. Precautions
The above simply tests the operation of adding a single piece of data. For other complex database operations how to implement, you can wait for the subsequent specific needs of the time to consider.
As can be seen from the above test, hibernate can completely block the operation of the database and operate the database completely by manipulating the class. There are advantages and disadvantages in this way. The advantage is that developers are relatively simple to use. The relative disadvantage is also obvious: no specific SQL statements can be authorized or flexible to manipulate SQL statements.
In addition, we need to be aware of security issues when working with databases, most notably SQL injection. There are several ways to avoid coding into SQL injection pitfalls. One is to filter the input string type data and filter out the obvious dangerous characters, which requires enough knowledge of SQL injection and is suitable for advanced developers. Second, the SQL statement preprocessing, the filtering operation to the underlying library to achieve, we only need to pre-processing the corresponding fields to the SQL query statement.
Hibernate is also possible to implement SQL preprocessing, we need to define the SQL query statement, here is an example for reference:
SQLQuery query = Session.createsqlquery ("SELECT * from note where id =?") ); // set the value of the first parameter to 12, which is the note for query id=12 Query.setparameter (0, n= Query.list ();
Using Hibernate database framework guidance in idea