Simple Example of Hibernate, simple example of Hibernate
First, we can create a new project, Java project or Javaweb project, and then import the jar required by hibernate. The jar package I use is as follows:
Create an object class Teacher. java and add the get and set methods:
1 public class Teacher { 2 private String username; 3 private String password; 4 private int age; 5 public String getUsername() { 6 return username; 7 } 8 public void setUsername(String username) { 9 this.username = username;10 }11 public String getPassword() {12 return password;13 }14 public void setPassword(String password) {15 this.password = password;16 }17 public int getAge() {18 return age;19 }20 public void setAge(int age) {21 this.age = age;22 }23 }
In Hibernate, an object class corresponds to a. hbm. xml file. Therefore, we create a new Teacher. hbm. xml file with the following content:
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6
The rows 9-11 represent the configuration of the primary key, the name refers to the fields in the object class, and the column refers to the corresponding fields in the database. In <genetator>, class = "assigned" indicates that the primary key does not automatically grow. The <property> below is the configured non-primary key. This file and the object class are in the same package.
Then we need to create a new hibernate. cfg. xml file under the src directory and import the Teacher. hbm. xml file corresponding to the object class. The Code is as follows:
1 <! DOCTYPE hibernate-configuration PUBLIC 2 "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 5
The basic information of the database should be configured for 7-12 behaviors. For example, you need to change lines 11 and 12 to the username and password of your database.
Next we will write a query method and the insertion method TeacherDao. java:
1 public class TeacherDao {2 // read the Configuration file 3 Configuration congif = new Configuration (). configure (); 4 // create sessionFactory 5 SessionFactory sf = congif. buildSessionFactory (); 6 // create session 7 Session session = null; 8 // create a Transaction (Hibernate must use a Transaction) 9 Transaction tx = null; 10 // insert a data record 11 public void insert (Teacher teacher) {12 try {13 // open session14 session = sf. openSession (); 15 // start transaction 16 tx = session. beginTransacti On (); 17 // perform persistent operations (add, delete, update) 18 sessions. save (teacher); 19 // submit transaction 20 tx. commit (); 21} catch (Exception e) {22 // transaction rollback 23 tx. rollback (); 24} finally {25 if (session! = Null) 26 session. close (); 27} 28 29} 30 // query data 31 public List <Teacher> find (String hql) {32 List <Teacher> teacher = new ArrayList <Teacher> (); 33 try {34 session = sf. openSession (); 35 tx = session. beginTransaction (); 36 // use the hql statement to Query 37 query Query = session. createQuery (hql); 38 // The list39 teacher = query is returned. list (); 40 tx. commit (); 41} catch (Exception e) {42 tx. rollback (); 43} finally {44 if (session! = Null) 45 session. close (); 46} 47 return teacher; 48} 49}
Of course, if the name of hibernate. cfg. xml in the src directory is not the same, you need to add the path of the configuration file when reading the configuration file in Row 3. Let's take a look at the directory structure of the file:
We will Test in the Test. java class. The Code is as follows:
1 public class Test {2 3 public static void main (String [] args) {4/** 5 * use the save method of Hibernate for persistence operations 6 */7 Teacher t = new Teacher (); 8 t. setUsername ("username4"); 9 t. setPassword ("root4"); 10 t. setAge (20); 11 TeacherDao dao = new TeacherDao (); 12 dao. insert (t); 13/** 14 * use the hql statement to query 15 * @ return: output the username of the returned result in the console 16 */17 String hql = "from Teacher"; 18 List <Teacher> teacher = dao. find (hql); 19 for (int I = 0; I <teacher. size (); I ++) {20 System. out. println (teacher. get (I ). getUsername (); 21} 22} 23 24}
Of course, do not forget the tables in the database. We can create a table based on the fields in Teacher. java. After that, we can run it in Test. java to check the results.