Hibernate簡要介紹
Hibernate存入數據庫首先要跟Configuration這個類打交道,通過configuration的buildSessionFactory來得到一個SessionFactory,然後拿到一個Session對象,然後直接調用save方法存入數據庫就行了。
這就是面向對象的寫法就是o/r-mapping o是object r是relationship是關係,是面向關係。Hibernate幫我們屏蔽了關係這一層。
Hibernate配置
首先去hibernate的官方網站www.hibernate.org去下載hibernate-distribution
由於要引進的包比較多,所以可以新建一個包含許多包的library,方法是:點擊Windows-Preferences-Java-Build-User Libraries,新new一個取名爲hinernate,然後就是加入jar包了。
我是剛去hibernate的官網上下的最新的包3.5.3final版,先把hibernate3.jar這個包加入進來,然後進入lib目錄,將裏面的required目錄下的所有包加進來,由於裏面使用了slf4j,注意這時候裏面加減來的slf4j-api-1.5.8.jar裏面的只是一些函數的聲明,就相當於一個接口。你還要去網上下對應版本的slf4j,下下來之後把slf4j-nop-1.5.8.jar這個包包含進來,這個包裏面的都是實現。
由於要和數據庫進行聯繫所以還有一個數據庫的jar包,添加你自己的數據庫的jar包,我的是mysql的jar包
下面我們寫一個演示的程序。
在myeclipse下新建一個java project,我的命名爲Hibernate_Show,新建一個student類,包名隨便你取。
student類如下:
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
新建一個hibernate.cfg.xml文件
對於這個配置文件最好自己去自己下的那個hibernate的包裏面的說明文檔裏面去拷貝
我的目錄是這樣的documentation/manual/en-US/html-single下的index.html
去setup裏面1.1.4 hibernate configuration下面的配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property>-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
名字叫做hibernate.cfg.xml,這個名字最好不要亂改,就這個名字,放在根目錄中。
首先看第一項,很明顯是數據庫連接的配置,你使用的是說明數據庫這個自己改,還有裏面的一些配置,比如是否使用二級緩存,以及顯示sql語句等,這個看你自己的需要。
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.model">
<class name="Student" table="student">
<id name="id" />
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>
貝
裏面的標籤一看比較清楚<class>就是你的類名,id這個標籤是來映射你的主鍵,property是一些屬性名,非主鍵屬性。這個hibernate就知道怎麼樣把class和表對應到一起了。
在hibernate.cfg.xml文件中最後一項<mapping>這個裏面是你的類與數據庫的對應關係。把你剛纔寫好的對應關係配置文件必須放在這裏,否則hibernate不好找。
下面來寫測試程序,新建一個類StudentTest.java,如下:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
public static void main(String args[]){
Student s = new Student();
s.setId(4);
s.setName("s4");
s.setAge(4);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
其中cfg.configure()裏面可以傳參數,我這裏沒有傳是因爲默認是解析根目錄下面的hibernate.cfg.xml文件,裏面的beginTransaction函數是因爲hibernate的處理都是放在一個事務中進行處理的。
Ok運行起來,最後可以在mysql中查看數據庫中是不是多了一項數據。