I mentioned hibernate integration when I learned about the database when I was learning Spring. However, I have almost no idea about hibernate technology, I only know that it is an orm object ing framework, so I will give a brief understanding of hibernate and Its Application in the preliminary chapter. By the way, I use the hibernate Development Kit added by maven.
General steps for using hibernate
* Create and edit the hibernate configuration file
This configuration file is mainly used to connect to the database. It defines the database driver and the location of the ing file. The following is a simple configuration.
<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
According to this configuration file, all configuration information is included in the
The show_ SQL attribute is a boolean value, which indicates whether to output SQL statements on the console during database operations. It is useful for debugging.
Connection. driver_class indicates the jdbc Driver of the database. I use mysql here, so it is org. gjt. mm. mysql. Driver.
Connection. url indicates the url used to access the database.
Connection. username indicates the username used to log on to the database.
Connection. password indicates the password used to log on to the database.
Dialect represents a database dialect. You can set some default values for the database attributes. (I understand this currently. I will study it further later) I know that the content of a webpage is the dialect class corresponding to the database. For details, refer to Baidu Library address.
<Mapping resource = ""/> the tag is used to reference the address of the ing file. The content of the ing file is briefly described below.
* Create a database table
The userid, name, and password fields of the database tables for creating a user are described as follows:
* Create a javaBean class corresponding to the database table
The following is a simple javaBean
package com.springframework.hibernate;public class User {private String userid;private String name;private String password;public String getUserid() {return userid;}public void setUserid(String userid) {this.userid = userid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
* Create a ing File
The ing file contains some configuration information. The Code will introduce the meaning of the configuration information one by one. The configuration file is as follows:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
The above database table contains three fields. The userid is the primary key during the period, and I will explain the meaning of the configuration file one by one.
All configuration information is written in the
<Class> the name attribute in a tag represents the javaBean corresponding to the class tag, and the table attribute represents the database table corresponding to the class tag.
<Id> labels are used to map primary keys. The user table's primary key is userid, the name attribute is the name of the attribute in javaBean, and the column attribute represents the fields in the database table.
<Generator> the label defines how to generate a primary key, which is called a generator. The selected method is assigned. The program generates a primary key freely. There are other centralized methods, I found a blog with a detailed address.
<Property> similar to Spring injection of javaBean, the reference column attribute of the database table field corresponding to the definition attribute represents the fields in the database table.
* Create a scenario class (main function)
The Code is as follows:
package com.springframework.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class Client {public static void main(String[] args) {try {SessionFactory sf = new Configuration().configure().buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();for (int i = 0; i < 100; i++) {User newuser = new User();newuser.setUserid("userid" + i);newuser.setName("name" + i);newuser.setPassword("password" + i);session.save(newuser);}tx.commit();session.close();} catch (Exception e) {e.printStackTrace();}}}
This is similar to a sample code, which can be replaced by a Spring template.
Hibernate starts hibernate through SessionFactory (connects to the data through the configuration file). The session object contains some database operations.
The above are some basic methods for using hibernate. after learning the above knowledge, you can continue to learn Spring, and then complete the practical notes about hibernate.