Using hibernate to link MySql to add data

Posted by DESIGNGRAPHY on Thu, 02 Apr 2020 05:38:07 +0200

Development tool: MyEclipse 2013, database: MySql

1. First, create a database in the database. The database tool I use is sqllog

Create the following database:



Open MyEclispe after database creation

2. Create Web Project

2.1: Item 1: Guide Package

The following packages need to be imported:(these bags can be found on the Internet, and I will share them in my resources)


2.2: write configuration file: hibernate.cfg.xml

  1. <span style="white-space:pre;">     </span><hibernate-configuration>  
  2.         <!-- Configuration database parameters -->  
  3.         <session-factory>  
  4.             <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  5.             <property name="connection.url">jdbc:mysql://localhost:3306/hibertest?characterEncoding=utf-8</property>  
  6.             <property name="connection.username">root</property>  
  7.             <property name="connection.password">sasa</property>  
  8.             <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  9.   
  10.   
  11.             <!-- display sql Sentence -->  
  12.             <property name="show_sql">true</property>  
  13.             <!-- formatting code  -->  
  14.             <property name="format_sql">true</property>  
  15.           
  16.             <mapping resource="com/entity/Dept.hbm.xml" />  
  17.         </session-factory>  
  18.         </hibernate-configuration>  

2.3: create an entity class and give the get/set method with or without parameters

  1. package com.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Dept implements Serializable {  
  6.   
  7.     private int deptno;  
  8.     private String deptname;  
  9.     private String loc;  
  10.   
  11.     public int getDeptno() {  
  12.         return deptno;  
  13.     }  
  14.   
  15.     public void setDeptno(int deptno) {  
  16.         this.deptno = deptno;  
  17.     }  
  18.   
  19.     public String getDeptname() {  
  20.         return deptname;  
  21.     }  
  22.   
  23.     public void setDeptname(String deptname) {  
  24.         this.deptname = deptname;  
  25.     }  
  26.   
  27.     public String getLoc() {  
  28.         return loc;  
  29.     }  
  30.   
  31.     public void setLoc(String loc) {  
  32.         this.loc = loc;  
  33.     }  
  34.   
  35.     public Dept(int deptno, String deptname, String loc) {  
  36.         super();  
  37.         this.deptno = deptno;  
  38.         this.deptname = deptname;  
  39.         this.loc = loc;  
  40.     }  
  41.   
  42.     public Dept() {  
  43.         super();  
  44.         // TODO Auto-generated constructor stub  
  45.     }  
  46.   
  47. }  

The file Dept.hbm.xml should be created in the entity package;

The code is as follows:

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.   
  8.     <class name="com.entity.Dept" table="dept">  
  9.         <!-- Primary key mapping -->  
  10.         <id name="deptno" column="deptno">  
  11.             <!-- The strategy of generating primary key -->  
  12.             <generator class="native"></generator>  
  13.         </id>  
  14.         <!-- Here name Value corresponds to attribute in entity class -->  
  15.         <property name="deptname" column="deptname"></property>  
  16.         <property name="loc" column="loc"></property>  
  17.     </class>  
  18. </hibernate-m  

Write the test class as follows:

The code is as follows:

  1. package com.test;  
  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.   
  10. import com.entity.Dept;  
  11.   
  12. import junit.framework.TestCase;  
  13.   
  14. public class Test extends TestCase {  
  15.   
  16.     //Add Department  
  17.     public void testAdd() {  
  18.   
  19.         //Read hibernate configuration file  
  20.         Configuration config = new Configuration()  
  21.                 .configure("hibernate.cfg.xml");  
  22.   
  23.         //All configurations are registered with a class  
  24.         ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(  
  25.                 config.getProperties()).buildServiceRegistry();  
  26.   
  27.         //Get sessionfactory  
  28.         SessionFactory sf = config.buildSessionFactory(sr);  
  29.   
  30.         //Get session, interface to operate database  
  31.         Session session = sf.openSession();  
  32.   
  33.         //Create a department object  
  34.         Dept dept = new Dept();  
  35.         dept.setDeptname("Development Department");  
  36.         dept.setLoc("Wuhan");  
  37.   
  38.         //Open things  
  39.         Transaction tran = session.beginTransaction();  
  40.   
  41.         //Perform insert operation  
  42.         //try catch manually - right click - surround with  
  43.         try {    
  44.             session.save(dept);  
  45.             tran.commit();  
  46.         } catch (Exception e) {  
  47.             tran.rollback();  
  48.             e.printStackTrace();  
  49.         } finally {  
  50.             session.close();  
  51.         }  
  52.     }  
  53. }  
  1. The overall figure is as follows:
  1.   
  1. Run the testAdd() method in the test class:
  1. The < span style = "white space: pre;" > console will print out the sql statement as follows:
  1.   
  1. Hibernate:   
  2.     insert   
  3.     into  
  4.         dept  
  5.         (deptname, loc)   
  6.     values  
  7.         (?, ?)  




  1. Database data addition result:
  1.   

Topics: Hibernate Database Session xml