標籤:
今天我就寫一下怎麼搭建Hibernate的架構的~
步驟如下:
1. 擷取hibernate發布包
可以在github或者官網找找~
2.將hibernate依賴的jar整合到項目中
一共要9個jar包~
到這個目錄hibernate\hibernate-distribution-3.6.10.Final\lib\required找,有6個jar包,
有這些包還不夠,還需要到hibernate\hibernate-distribution-3.6.10.Final\lib\jpa這個目錄下,看圖:
還需要倆個jar包,一個是串連資料庫的包,一個是Hibernate包,
到這裡jar包全部找到,可以匯入到項目裡面了~
在項目中建立一個jar檔案,把要依賴的jar包放進去,:
jar包包括驅動包和hibernate3.jar和一些別的包,都是一些必須要的包~
3.將hibernate依賴的設定檔和對應檔加入到項目中
hibernate.cfg.xml 設定檔
***.hbm.xml 對應檔
這時候就要把dtd檔案放入到項目中了~
把hibernate3.jar包解壓開來,進入到hibernate\hibernate-distribution-3.6.10.Final\hibernate3\org\hibernate,可以找到倆個dtd檔案,
在項目中建立一個dtd檔案,把必要的dtd放入到dtd檔案中,
hibernate.cfg.xml設定檔的寫法:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- Database connection settings --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property><property name="connection.username">root</property><property name="connection.password">root</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property></session-factory></hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">這幾句可以從dtd檔案中hibernate-configuration-3.0.dtd找~,這幾句是必須要的
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property>中的ssh是在Native MySql中建立的一個資料庫的名字~
<hibernate-configuration>這行代碼出不現的話,就要配置了,在Window的Preferences做操作:
上面的location就是這個項目中dtd檔案的所在目錄,而key就是http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd這個連結~
這樣設定檔就配置好了
***.hbm.xml對應檔的寫法:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.xu.day4.basic"></hibernate-mapping>
<hibernate-mapping package="com.xu.day4.basic">是指要映射到哪個包下的哪個類~
同樣
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">這幾行代碼也是必須的~可以從hibernate-mapping-3.0.dtd檔案中找~
<hibernate-mapping>如果這行代碼也出不現的話,和設定檔的配置步驟是一樣的~
4.將對應檔整合到設定檔中
看:
只需要這樣整合就行~
那架構就搭配完成了~
現在就可以寫寫一個簡單的例子:
要做一個使用者的儲存~
User類:
public class User implements Serializable{private static final long serialVersionUID = 1L;Long id;String name;Double account;Date birthday;public User() {super();// TODO Auto-generated constructor stub}public User(Long id, String name, Double account, Date birthday) {super();this.id = id;this.name = name;this.account = account;this.birthday = birthday;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getAccount() {return account;}public void setAccount(Double account) {this.account = account;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", account=" + account + ", birthday=" + birthday + "]";}}
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- Database connection settings --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property><property name="connection.username">root</property><property name="connection.password">root</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property><mapping resource="com/xu/day4/basic/user.hbm.xml" /></session-factory></hibernate-configuration>
對應檔user.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.xu.day4.basic"><class name="User" table="tb_user"><id name="id" column="id"><generator class="increment" /></id><property name="name" /><property name="account" /><property name="birthday" /></class></hibernate-mapping>
在Test類中代碼運行入口:
public class Test {public static void main(String[] args) {// TODO Auto-generated method stub//1.建立設定物件Configuration configuration=new Configuration();//2.讀取設定物件configuration.configure();//3.根據設定檔資訊建立一級緩衝,SessionFactory/* * Connection * 對應檔,映射對象 * HttpSession 使用者與伺服器之間的會話 * Session 代碼與資料庫之間的會話 */SessionFactory ss=configuration.buildSessionFactory();//4.建立Session,開啟會話Session session=ss.openSession();//5.開啟事務Transaction tran=session.beginTransaction();session.save(new User(null, "張二", 100.0, new Date()));//6.事務的提交tran.commit();//7.session關閉session.close();System.out.println("1111111111111111111");}}注釋已經寫的很清楚,我就不一一解釋了~
下一篇我也講個例子,可以把部分代碼封裝起來~
我就不貼了,有什麼需要的可以和我交流啊~
源碼:下載
Hibernate 架構的詳細搭建與簡單使用(一)