1. 程式人生 > >使用hibernate連結MySql實現新增資料功能

使用hibernate連結MySql實現新增資料功能

開發工具: MyEclipse2013 , 資料庫: MySql

1.首先, 在資料庫中建立資料庫 , 我使用的資料庫工具是SQLyog.

建立如下資料庫:

資料庫建立完成後開啟MyEclispe

2.建立Web Project

2.1: 第一項: 導包

需要匯入如下包:(這些包在網上都可以找到, 我也會共享在我得資源裡)

2.2 : 編寫配置檔案:hibernate.cfg.xml

  1. <span style="white-space:pre;">     </span><hibernate-configuration>  
  2.         <!-- 配置資料庫各個引數 -->
      
  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.             <!-- 顯示sql語句 -->  
  10.             <property name="show_sql">true</property
    >  
  11.             <!-- 格式化程式碼 -->  
  12.             <property name="format_sql">true</property>  
  13.             <mapping resource="com/entity/Dept.hbm.xml" />  
  14.         </session-factory>  
  15.         </hibernate-configuration>  

2.3: 建立一個實體類 , 並給get/set方法 , 有參無參構造

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

實體包裡需建立Dept.hbm.xml檔案;

程式碼如下:

  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. <hibernate-mapping>  
  6.     <class name="com.entity.Dept" table="dept">  
  7.         <!-- 主鍵對映 -->  
  8.         <id name="deptno" column="deptno">  
  9.             <!-- 主鍵產生的值得策略 -->  
  10.             <generator class="native"></generator>  
  11.         </id>  
  12.         <!-- 此處的name值對應實體類中屬性 -->  
  13.         <property name="deptname" column="deptname"></property>  
  14.         <property name="loc" column="loc"></property>  
  15.     </class>  
  16. </hibernate-m  

下面編寫test類:

程式碼如下:

  1. package com.test;  
  2. import org.hibernate.Session;  
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.Transaction;  
  5. import org.hibernate.cfg.Configuration;  
  6. import org.hibernate.service.ServiceRegistry;  
  7. import org.hibernate.service.ServiceRegistryBuilder;  
  8. import com.entity.Dept;  
  9. import junit.framework.TestCase;  
  10. public class Test extends TestCase {  
  11.     // 增加部門  
  12.     public void testAdd() {  
  13.         // 讀取hibernate配置檔案  
  14.         Configuration config = new Configuration()  
  15.                 .configure("hibernate.cfg.xml");  
  16.         // 所有的配置都要向一個類中註冊  
  17.         ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(  
  18.                 config.getProperties()).buildServiceRegistry();  
  19.         // 獲得sessionfactory  
  20.         SessionFactory sf = config.buildSessionFactory(sr);  
  21.         // 獲得session,操作資料庫的介面  
  22.         Session session = sf.openSession();  
  23.         // 建立一個部門物件  
  24.         Dept dept = new Dept();  
  25.         dept.setDeptname("開發部");  
  26.         dept.setLoc("武漢");  
  27.         // 開啟事物  
  28.         Transaction tran = session.beginTransaction();  
  29.         // 執行插入操作  
  30.         //手動try catch 選中-右鍵 -surround with  
  31.         try {    
  32.             session.save(dept);  
  33.             tran.commit();  
  34.         } catch (Exception e) {  
  35.             tran.rollback();  
  36.             e.printStackTrace();  
  37.         } finally {  
  38.             session.close();  
  39.         }  
  40.     }  
  41. }  
  1. 總體圖如下:  
  1. 執行test類裡的testAdd()方法:  
  1. <span style="white-space:pre;"> </span>控制檯會打印出sql語句如下:  
  1. Hibernate:   
  2.     insert   
  3.     into  
  4.         dept  
  5.         (deptname, loc)   
  6.     values  
  7.         (?, ?)  
  1. 資料庫資料新增結果: