Java from getting started to giving up: Basic hibernate configuration,

Source: Internet
Author: User

Java from getting started to giving up: Basic hibernate configuration,

What is hibernate? Simply put, it is a set of database access interfaces written by others. The underlying layer is actually JDBC.

Using JDBC is like having no rice cooker in the past. We need to learn how many people to cook, how much water to put, how much fire to use, and how long to cook, you need to control the whole process by yourself. If you do not control well, if you are lucky, you just need to cook the rice into porridge. If you are lucky, it is just a pot of charcoal.

The use of hibernate is the same as the use of rice cookers, according to the scale of water and rice, then according to the cooking or porridge, then wait for it "ding ~" After a few clicks, you can eat it, not to mention how good it is to cook, at least it is cooked, and it is cooked !!!

Without years of experience in cooking, the former cannot cook enough food, and the grains are full of rice. Then, even the kitchen like me can cook delicious rice!

 

 

Here, the basic features of hibernate are clear.

First: easy to use !!!

Second: simple !!!

Third: simple and easy to use !!!

 

Okay, it's been a long time since niub started to get dry. The procedure is as follows:

  1. Use the built-in DBBrowser in myeclipse to create a database connection.

  2. Right-click the project and install the hibernate framework

  3. Find the corresponding table in DBBrowser and right-click to generate the object class.

  4. Write test code

 

1. Create a database connection in DBBrowser

 

2. Right-click the project and install the hibernate framework

After installation, three more files will be added to the project:

HibernateSessionFactory. java, which is used to obtain the Session object in Hibernate. Database Operations in Hibernate are completed by the Session object (note the Session in Hibernate ).

Hibernate. cfg. xml, which is used for Hibernate-related configuration, such as the four elements for connecting to the database, to display SQL statements on the console.

The mysql-connector-java-5.1.34-bin.jar in lib is the driver package that Java uses to access mysql.

 

3. Right-click the corresponding table in DBBrowser to generate an object class.

The explanations on the wizard are shown in the figure.

After the file is generated, drag the *. hbm. xml file to the dao layer package, and modify the project structure after the corresponding path in mapping in the hibernate. cfg. xml file as follows:

 

4. Rewrite the toString method of the object class and write the test code.(Since Junit has not been mentioned here, You can directly use the class with the main method for testing)

123456789101112 public class Test {    public static void main(String[] args) {        // Note that the Session is org. hibernate. Session.        Session session = HibernateSessionFactory.getSession();        // Load method: obtains object class objects based on the primary key.        Author author = (Author)session.load(Author.class1);                 System.out.println(author);                 HibernateSessionFactory.closeSession();    }}

The final result is as follows:

Now, the basic configuration of hibernate is OK! What are the meanings of the specific configuration files and how to configure them? We will see you next time!

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.