Hibernate, one of the three frameworks of SSH, is used to deal with the Dao layer of the program and the database. It encapsulates the JDBC steps and makes database operations easier and faster. With the Hibernate framework, we can stop writing repeated JDBC code and repeatedly test how our SQL statements are written. Here, we need simple configuration. by calling the methods provided by the framework, we can add, delete, modify, and query data. What kind of framework does Hibernate provide? How to use it? First, let's look at a knowledge system diagram about The Hibernate framework:
In this blog, We will summarize Hibernate's macro understanding and environment construction. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + examples/examples + examples/nT2tSqyv2 + 3 bXEx + examples/examples + examples/b7dtcTK/b7dKaOs0rK + examples/PK/example/Pws7Eu7e + examples + records + 7 rNSWJhdGlzz + CxyKOsy/records + records/records + jqbrNyv2 + 3b/records + bP8rbUz/PJ6LzGus252M + 10M3K/b7dv + records/4re9w + examples + PGJyPgo8L3A + examples + zcrHyc + examples/examples + example + 3b/example/examples + example + 3b/example + q82oo6zO0sPHu + HD5s/yttTP87Hgs8y + zb/example + examples/ examples/JysfO0r71tcTSqs/examples + examples/examples + IDwvcD4KPHA + examples/examples + examples/KGjamFysPy1xL + examples/examples + examples/LrDtcS527Lsv/examples + o6zL + examples/Examples rPJU1FM0 ++ 5Ck8L3A + cost + 3dS0way907PY1 + m8/ik8L3A + cost + 6z7mkvt/Cost/cost + 4us2/Cost/tcS12Ck8L3A + cost + m8/ik8L3A + cost/cost + cost/Cost/Ck8L3A + CjxwPiA8L3A + cost + o7o8L3A + cost/b7dv + cost/cost + cjxwixhbmc9 "en- US ">
3. Write the core configuration file hibernate. cfg. xml:
Jdbc: mysql: // localhost: 3306/example? UseUnicode = true & characterEncoding = utf8
Com. mysql. jdbc. Driver
Ljh
Ljh
True
True
Org. hibernate. dialect. MySQLDialect
4. *. hbm. xml corresponds to the object class for compiling ing files. I think this is the focus, and there are many knowledge points in design. Here is a simple example. The following will summarize the Syntax:
5. The ing between the object class and the data table has been completed. We can use the methods provided by The Hibernate framework to operate the data in the database. simply look at the implementation of a dao layer class:
Public void insert (User user) {// load the configuration file: classpath: hibernate. cfg. xmlConfiguration config = new Configuration (). configure (); // create the connection factory SessionFactory sf = config. buildSessionFactory (); // Session object, indicating the connection session process with the database, which is equivalent to the jdbc connection object Session = null; Transaction tx = null; try {// obtain the session object session = sf. openSession (); // open the transaction: Hibernate does not commit the transaction by default for data operations, so we need to submit the transaction manually. Tx = session. beginTransaction (); // Save the object and operate the session persistently. save (user); // submit the transaction tx. commit ();} catch (Exception e) {e. printStackTrace (); // transaction rollback tx. rollback ();} finally {// connection session close if (session! = Null) {session. close ();}}}
As a result, we have written a lot of code in our method. Here we just wrote the Hibernate framework from creating the sessionFactory object and then creating the session object, perform database operations through the session object. In fact, in real development, we will hand over these objects and transaction operations to the framework for management. What we need to do is the code for persistent operations, which is very few.
To sum up, we will implement a simple example of inserting user data for Hibernate. Of course, if the database relationship is complicated or more settings, we will summarize them later. Finally, let's take a look at the advantages and disadvantages of the Hibernate framework. Of course, these advantages and disadvantages are relative. Let's look at the environment used!
Advantages: 1. Lightweight ORM framework
2. The JDBC is well encapsulated and the orm ing is used, so the database can be easily operated through the object-oriented method.
3. It also provides a cache mechanism to improve efficiency. And so on
Disadvantages: 1. If you perform frequent operations on a large amount of data and the performance is relatively low, it is better to directly use JDBC.
All in all, for the framework, we can use its excellent side to avoid its bad side, so that it can play the biggest role in our development !!!