Hibernate簡述及入門執行個體,hibernate簡述執行個體
一.Hibernate簡述
總的概括,Hibernate是一個ORM的輕量級持久層架構,解決了對象和關聯式資料庫中表的不匹配問題(阻抗不匹配)以及擁有開發代碼不用去繼承hibernate類或介面的優勢(無侵入性)。hibernate架構實現使得開發人員可以避免反覆地編寫javajdbc部分代碼,應用物件導向的思維操作關係型資料庫。
二.使用myeclipse建立hibernate執行個體兩種方法(以hibernate3.5.2及mysql為例)a)手動編寫hibernate.cfg.xml及*.hbm.xml檔案1.建立java項目,匯入相應所需jar包(lib\required檔案夾內所有jar包+mysql相應jar包+hibernate3.jar包)到java項目下建立的lib檔案夾中,選中所有jar右鍵Build Path->Add Build Path,在src檔案夾下建立一個pro包test包和hibernate.cfg.xml檔案
2.在pro包下建Student實體類(PS:myeclipse自動產生get/set方法:Ctrl+Shift+s -> Generate Getters and Setters)
1 package test.hibernate.pro; 2 3 public class Student { 4 int id; 5 int age; 6 String name; 7 int classid; 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 public int getClassid() { 27 return classid; 28 } 29 public void setClassid(int classid) { 30 this.classid = classid; 31 } 32 }
3.使用MysqlGUI工具或cmd視窗建表(也可使用hibernate的SchemaExport方法自動產生表結構)。這裡使用cmd視窗,進入mysql檔案夾下lib目錄輸入mysql –hlocalhost –u root –p,之後Enter Password進入資料庫create建表
4.在pro包下建*.hbm.xml檔案匯入標頭檔,完成object與表結構的映射關係
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="test.hibernate.pro.Student" table="student"> 7 <!-- name值為對象屬性名稱,column值為相應列名 ,相互對應 --> 8 <id name="id" column="stu_id"> 9 <!-- 主鍵建置原則 --> 10 <generator class="native"></generator> 11 </id> 12 <!-- 實體類屬性 --> 13 <property name="age" column="stu_age"></property> 14 <property name="name" column="stu_name"></property> 15 <property name="classid" column="stu_classid"></property> 16 </class> 17 </hibernate-mapping>
5.配置hibernate.cfg.xml檔案
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 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 配置資源資訊 --> 9 <!-- mysql預設連接埠號碼為3306 --> 10 <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 <property name="format_sql">true</property> 16 <property name="show_sql">true</property> 17 18 <!-- 資料庫方言 --> 19 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 20 21 <!-- 註冊調用*。hbm.xml檔案 --> 22 <mapping resource="test/hibernate/pro/Student.hbm.xml"></mapping> 23 </session-factory> 24 </hibernate-configuration>
6.運行test包下test.java測試
1 package test.hibernate.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 8 import test.hibernate.pro.Student; 9 10 public class test { 11 public static void main(String[] args) { 12 Configuration cfg = new Configuration().configure(); 13 14 // hibernate3.5寫法 15 SessionFactory sf = cfg.buildSessionFactory(); 16 // hibernate4.0 -hibernate4.2 寫法 17 // ServiceRegistry reg = new 18 // ServiceRegistryBuilder().applySettings(cfg.getProperties()) 19 // .buildServiceRegistry(); 20 // hibernate4.3 寫法 21 // ServiceRegistry reg = new 22 // StandardServiceRegistryBuilder().applySettings(cfg.getProperties()) 23 // .build(); 24 // SessionFactory sf = cfg.buildSessionFactory(reg); 25 26 Session session = null; 27 Transaction ta = null; 28 try { 29 session = sf.openSession(); 30 ta = session.beginTransaction(); 31 32 Student stu = new Student(); 33 stu.setAge(17); 34 stu.setName("Neo"); 35 stu.setClassid(2); 36 session.save(stu); 37 38 ta.commit(); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 if (ta != null) { 42 ta.rollback(); 43 } 44 } finally { 45 if (session != null && session.isOpen()) { 46 session.close(); 47 } 48 } 49 } 50 } b)使用myeclipse自動產生hibernate.cfg.xml及*.hbm.xml檔案
1.建立java web項目,src目錄下建pro,factory,dao,test包
2.開啟工作列window -> Show View -> DB Browser,匯入mysqljar包建立mysql jdbc Driver。右鍵項目 -> MyEclipse -> ProjectFacets -> Install Hibernate Facet建立hibernate.cfg.xml及SessionFactory檔案命名為SF.java
3.DB Browser中右鍵表結構Hibernate Reverse Engineering反向產生實體類到pro包,選項三個勾一個點,選擇主鍵增長策略,執行個體中選擇native(預設為native)
4.dao包中建立dao層類
1 package test.hibernate.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import test.hibernate.factory.SF; 7 import test.hibernate.pro.Student; 8 9 public class StudentDao { 10 Session session = null; 11 Transaction ta = null; 12 13 public void addStudent(Student stu) { 14 try { 15 session = SF.getSession(); 16 ta = session.beginTransaction(); 17 session.save(stu); 18 ta.commit(); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 if (ta != null) 22 ta.rollback(); 23 } finally { 24 if (session != null) 25 session.close(); 26 } 27 } 28 }
5.test包中建立test測試類別測試
1 package test.hibernate.test; 2 3 import test.hibernate.dao.StudentDao; 4 import test.hibernate.pro.Student; 5 6 public class Test { 7 public static void main(String[] args) { 8 StudentDao sd = new StudentDao(); 9 10 Student stu = new Student(); 11 stu.setStuAge(19); 12 stu.setStuName("Mike"); 13 stu.setStuClassid(1); 14 15 sd.addStudent(stu); 16 } 17 }
總結,hibernate實現了orm對象關係映射,以對象形式對資料庫資料進行增刪改查的操作從而提升了編程的效率。