標籤:style blog http color java 使用 os 檔案
Hibernate是什麼
Hibernate是一個輕量級的ORMapping架構
ORMapping原理(Object Relational Mapping)就是把對象裡面的資料和資料庫裡面的資料,按照一定的規則進行映射的過程。
ORMapping基本對應規則:
1:類跟表相對應
2:類的屬性跟表的欄位相對應
3:類的執行個體與表中具體的一條記錄相對應
Hibernate的實現方式
Hibernate解決的問題:
通過我們也能發現,Hibernate主要用來實現Java對象和表之間的映射,除此之外還提供資料查詢和擷取資料的方法,可以大幅度減少開發時人工使用SQL和JDBC處理資料的時間。
Hibernate的目標是對於開發人員通常的資料持久化相關的編程任務中解放出來。
Hibernate可以協助你消除或者封裝那些針對特定廠商的SQL代碼,並且幫你把結果集從表格式的表示形式轉換到一系列的對象去。
hibernate優點:
1、封裝了jdbc,簡化了很多重複性代碼。
2、簡化了DAO層編碼工作,使開發更對象化了。
3、移植性好,支援各種資料庫,如果換個資料庫只要在設定檔中變換配置就可以了,不用改變hibernate代碼。
4、支援透明持久化,因為hibernate操作的是純粹的(pojo)java類,沒有實現任何介面,沒有侵入性。所以說它是一個輕量級架構。
下面結合應用執行個體協助大家理解:
實體類:
import java.util.Date;public class User {private String id;private String username;private String userpassword;private String createTime;private String expireTime;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String name) {this.username = name;}public String getUserpassword() {return userpassword;}public void setUserpassword(String password) {this.userpassword = password;}public String getCreateTime() {return createTime;}public void setCreateTime(String createTime) {this.createTime = createTime;}public String getExpireTime() {return expireTime;}public void setExpireTime(String expireTime) {this.expireTime = expireTime;}}
建立hibernate對應檔User.hbm.xml
對應檔告訴Hibernate它應該訪問資料庫裡面的哪個表(table)和應該使用表裡面的哪些欄位(column)。
因為這裡只有一個Class ---User和一個Table --- t_user,你只需要建立一個對應檔---User.hbm.xml,來對應User類和t_user表之間的關係。
<hibernate-mapping package="com.bjpowernode.hibernate"><class name="User" table="t_user"><id name="id"><generator class="uuid"></generator></id><property name="username"></property><property name="userpassword"></property><property name="createTime"></property><property name="expireTime"></property></class></hibernate-mapping>
配置Hibernate描述檔案hibernate.cfg.xml
Hibernate描述檔案可以是一個properties或xml檔案,其中最重要的是定義資料庫的串連。我這裡列出的是一個XML格式的hibernate.cfg.xml描述檔案。
<hibernate-configuration><session-factory ><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">hejingyuan</property><!-- 方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show.sql">true</property><property name="hibernate.show.format_sql">true</property><!-- 指定User的對應檔 --><mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/></session-factory></hibernate-configuration>
產生表方法
/** * 將hbm產生ddl * @author Administrator * */public class ExportDB {public static void main(String[] args) {//預設讀取hibernate.cfg.xml檔案Configuration cfg = new Configuration().configure();SchemaExport export = new SchemaExport(cfg);export.create(true, true);}}
添加使用者
public static void main(String[] args) {//讀取hibernate.cfg.xml檔案--預設預設檔案,如果只new的話,會讀取porpertiesConfiguration cfg=new Configuration().configure();//建立SessionFactory,安全執行緒的,最好一個資料庫建立一次SessionFactory factory=cfg.buildSessionFactory();//取得Session,真正要用的時候才去關聯connection,這個connection與我們之前學的不同,稱為持久化管理器Session session=null;try{session=factory.openSession();//開啟事務session.beginTransaction();User user=new User();user.setUsername("張三");user.setUserpassword("123");user.setCreateTime(new Date().toString());user.setExpireTime(new Date().toString());//儲存User對象session.save(user);//提交事務session.getTransaction().commit();}catch(Exception e){e.printStackTrace();//復原事務session.getTransaction().rollback();}finally{if(session != null){if(session.isOpen()){//關閉sessionsession.close();}}}}
總結:
千裡之行始於足下,這篇文章僅是邁向Hibernate大道的一個起點。以上只是簡單介紹了Hibernate解決的問題,其實Hibernate就是一個轉換器,完成對象執行個體與資料庫表的轉換,對JDBC訪問資料庫的代碼進行封裝,簡化了資料訪問層繁瑣的重複性代碼。
下篇繼續!