What's a hibernate thing? Simply put, is someone else to write a good set of access to the database, the bottom is actually jdbc.
Using JDBC like no rice cooker before, we have to ourselves according to experience, how many people boil how many meters, put how much water, with how much fire, boil how long, the whole process need to control themselves, if the control is not good, luck just boiled rice into porridge, bad luck is a pot of charcoal.
and use Hibernate with the same as the use of rice cooker, according to the scale of water and rice, and then according to cooking rice or porridge, then it "ding ~" After a sound, you can eat, do not say how good boiled, at least boiled out is rice, and is cooked!!!
The former does not have many years of cooking experience must not be able to cook a full taste, grain full of rice, and then boil even if it is like me the kitchen small white can also cook fragrant spray delicious rice!
Here, the basic features of hibernate are clear.
First: Easy to use!!!
Second: Simple!!!
Third: simple and useful!!!
OK, cow B has been blowing for half a day and started to dry. The following steps are used:
-
Create a good one database connection first using the Dbbrowser that comes with MyEclipse
-
Right click on project, install hibernate frame
-
Find the corresponding table in Dbbrowser and right-click to generate the entity class.
-
Writing test Code
I. Creating a database connection in Dbbrowser
Second, right-click on the project to install the Hibernate framework
After installation, there are three more files in the project:
Hibernatesessionfactory.java, which is used to get the session object in hibernate, the database operation in Hibernate is done by the session object ( Note that this is the session in Hibernate).
Hibernate.cfg.xml, this is used for hibernate-related configuration, such as the four elements connected to the database, to display SQL statements in the console.
The Mysql-connector-java-5.1.34-bin.jar in Lib is the driver package that Java uses to access MySQL.
Third, right-click on the corresponding table in Dbbrowser to generate the entity class.
The explanations on the guide are all on the map and look at Kazakhstan.
After the build is finished, drag the *.hbm.xml file to the DAO layer package, and then modify the corresponding path in the mapping in the hibernate.cfg.xml file after the project structure is as follows:
rewrite the ToString method of the entity class, and then write the test code (because there is no junit in here, so use the class with the main method to test it directly)
| 123456789101112 |
publicclassTest { publicstaticvoidmain(String[] args) { //注意Session是org.hibernate.Session Session session = HibernateSessionFactory.getSession(); //load方法:根据主键得到实体类对象 Author author = (Author)session.load(Author.class, 1); System.out.println(author); HibernateSessionFactory.closeSession(); }} |
The final result is as follows:
OK, here's the basic configuration of hibernate OK! Specific several configuration files inside the content to indicate what meaning, how to configure, we next time goodbye!
"Getting Started from Java to quitting": Hibernate basic Configuration