Talking about Hibernate-getting started
What is Hibernate?
Hibernate is a lightweight ORMapping framework.
The ORMapping principle (Object Relational Mapping) is to map the data in the Object to the data in the database according to certain rules.
ORMapping basic rules:
1: The class corresponds to the table
2: The class attributes correspond to the table fields.
3: the instance of the class corresponds to a specific record in the table.
Hibernate Implementation Method
Problems solved by Hibernate:
We can also find that Hibernate is mainly used to implement ing between Java objects and tables. In addition, it also provides data query and acquisition methods, this greatly reduces the time required to manually process data using SQL and JDBC during development.
The goal of Hibernate is to free developers from the usual data persistence-related programming tasks.
Hibernate can help you eliminate or wrap the SQL code for specific vendors, and help you convert the result set from table-based representation to a series of objects.
Hibernate advantages:
1. it encapsulates jdbc and simplifies a lot of repetitive code.
2. simplified the coding of the DAO layer and made development more object-oriented.
3. Good portability and support for various databases. If you change a database, you only need to change the configuration in the configuration file. You do not need to change the hibernate code.
4. Transparent persistence is supported because hibernate operates purely (pojo) java classes without implementing any interfaces and is not invasive. Therefore, it is a lightweight framework.
The following examples help you understand:
Entity class:
import java.util.Date;public class User {private String id;private String username;private String userpassword;private String createTime;private String expireTime;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String name) {this.username = name;}public String getUserpassword() {return userpassword;}public void setUserpassword(String password) {this.userpassword = password;}public String getCreateTime() {return createTime;}public void setCreateTime(String createTime) {this.createTime = createTime;}public String getExpireTime() {return expireTime;}public void setExpireTime(String expireTime) {this.expireTime = expireTime;}}
Create the hibernate ing file User. hbm. xml
The ing file tells Hibernate which table in the database should be accessed and which fields in the table should be used (column ).
Because there is only one Class --- User and one Table --- t_user, you only need to create a ing file --- User. hbm. xml to correspond to the relationship between the User Class and the t_user Table.
Configure the Hibernate description file hibernate. cfg. xml
The Hibernate description file can be a properties or xml file, the most important of which is to define the database connection. Here is a hibernate. cfg. XML description file in xml format.
Com. mysql. jdbc. Driver
Jdbc: mysql: // localhost: 3306/hibernate_first
Root
Hejingyuan
Org. hibernate. dialect. MySQLDialect
True
True
Table Generation Method
/*** Generate ddl * @ author Administrator for hbm **/public class ExportDB {public static void main (String [] args) {// hibernate is read by default. cfg. xml file Configuration cfg = new Configuration (). configure (); SchemaExport export = new SchemaExport (cfg); export. create (true, true );}}
Add User
Public static void main (String [] args) {// read hibernate. cfg. xml file -- default file. If it is only new, it will read porpertiesConfiguration cfg = new Configuration (). configure (); // set up SessionFactory. Thread-safe. The best way is to create SessionFactory factory = cfg once in a database. buildSessionFactory (); // gets the Session and associates it with the connection only when it is actually used. This connection is different from what we have learned before, called the persistence Manager Session session = null; try {session = factory. openSession (); // start the transaction session. beginTransaction (); User user = new User () ; User. setUsername (zhangsan); user. set userpassword (123); user. setCreateTime (new Date (). toString (); user. setExpireTime (new Date (). toString (); // Save the session of the User object. save (user); // submit the transaction session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace (); // roll back the transaction session. getTransaction (). rollback ();} finally {if (session! = Null) {if (session. isOpen () {// close sessionsession. close ();}}}}
Summary:
The journey of a thousand miles begins with a single step. This article is only a starting point for the Hibernate Avenue. The above is just a brief introduction to the problem solved by Hibernate. In fact, Hibernate is a converter, which converts object instances and database tables and encapsulates the code for accessing the database through JDBC, simplifies the tedious and repetitive code at the data access layer.