Hibernate-chapter two, hibernate-chapter
Meet again !!! After learning in the previous chapter, we have some knowledge about the Hibernate framework. Next we will go to the second stage-practice!
======Prepare Database======
MySQL is used as an example.
For example:
Database operations are now here.
Ps: if you do not have a database base, Please study it first!
======Prepare the Hibernate jar package======
As for the version you want to download, use 4.2.4 as an example.
* There is a database driver package <mysql-connector-......>. You can download the database driver package for any database you use. Here is MySQL.
======Create a project======
======Next is the encoding stage.======
The steps are clearly written. I will not explain them any more, so let's proceed.
**************************************** ***************
Figure: code structure diagram
1 -- create a Hibernate configuration file
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE hibernate-configuration PUBLIC 3 "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5
2 -- create a persistence class
1 package com.jbit.entity.User; 2 public class User { 3 4 private int id; 5 private String name; 6 private int age; 7 8 public int getId() { 9 return id;10 }11 public void setId(int id) {12 this.id = id;13 }14 public String getName() {15 return name;16 }17 public void setName(String name) {18 this.name = name;19 }20 public int getAge() {21 return age;22 }23 public void setAge(int age) {24 this.age = age;25 }26 public User() {27 28 }29 public User(String name, int age) {30 super();31 this.name = name;32 this.age = age;33 }34 @Override35 public String toString() {36 return "User [id=" + id + ", name=" + name + ", age=" + age + "]";37 }38 39 40 41 42 }
3 -- create a link ing File
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5
4 -- create database access code through Hibernate APL
1 package com. jbit. entity. user; 2 3 import org. hibernate. session; 4 import org. hibernate. sessionFactory; 5 import org. hibernate. transaction; 6 import org. hibernate. cfg. configuration; 7 import org. hibernate. service. serviceRegistry; 8 import org. hibernate. service. serviceRegistryBuilder; 9 import org. junit. test; 10 11 public class HibernateTest {12 @ Test13 public void test () {14 15 System. out. println (" Test... start ...... "); 16 17 // 1. create a SessionFactory object 18 19 SessionFactory sessionFactory = null; 20 21 // 1 ). create a Configuration object: corresponds to the basic Configuration information of hibernate and object link ing information 22 configuration Configuration = new Configuration (). before configure (); 23 24 // 4.0, create 25 // sessionFactory = configuration. buildSessionFactory (); 26 27 // 2 ). create a ServiceRegistry object: hibernate 4.x newly added object 28 // any configuration and service of hibernate must be registered in this object before it becomes effective. 29 ServiceRegistry serviceRegistry = 30 new ServiceRegistryBuilder (). applySettings (configuration. getProperties () 31. buildServiceRegistry (); 32 33 // 3 ). 34 sessionFactory = configuration. buildSessionFactory (serviceRegistry); 35 36 // 2. create a Session object 37 Session session = sessionFactory. openSession (); 38 39 // 3. enable Transaction 40 transaction Transaction = session. beginTransaction (); 41 42 // 4. execute the save operation 43 User users = new User ("Java12345", 10); 44 45 session. save (users); 46 47 48 // 5. commit transaction 49 transaction. commit (); 50 51 // 6. disable Session52 session. close (); 53 54 // 7. close SessionFactory object 55 sessionFactory. close (); 56} 57}
Well ------------- after completing the above knock and understanding, we will execute the command to check whether the database automatically adds tables and columns to the database!
Check my success! What about you ???
Jun ------- Guang Jun from Nima planet/QQ: 383996901