Hibernate簡介
hibernate是一個開源的ORM(對象關係映射)框架,也是一個持久層的框架,它對JDBC做了非常輕量級的封裝。
Hibernate核心(六大接口+配置文件)
Configuration:負責配置並啓動Hibernate
SessionFactory:負責初始化Hibernate
Session:負責持久化對象的CRUD操作
Transaction:負責事務
Query和Criteria:負責執行各種數據庫查詢
Hibernate工作原理
- 通過Configuration config = new Configuration().configure();//讀取並解析hibernate.cfg.xml配置文件
- 由hibernate.cfg.xml中的<mapping resource = "com/xx/xx.hbm.xml" />//讀取並解析映射信息
- 通過SessionFactory sf = config.buildSessionFactory();//創建SessionFactory會話工廠
- Session session = sf.openSession();//打開Session
- Transaction tx = session.beginTransaction();//創建並啓動事務
- persistent operate CRUD//數據持久化操作
- tx.commit();//提交事務
- 關閉Session
- 關閉SesstionFactory
開發步驟:
- 創建Hibernate的配置文件
- 創建持久化類
- 創建對象-關係映射文件
- 通過Hibernate API編寫訪問數據庫的代碼
Hibernate開發實例
- 使用版本:Hibernate 4.2.4 + Mysql
- 導入Hibernate必須的jar包(Hibernate-release-4.2.4.Final\lib\required)
- 導入Mysql的jdbc驅動(mysql-connector-java-5.1.7-bin.jar)
- 導入Junit4的jar包(Junit單元測試工具)//測試用
- 創建Hibernate的配置文件 hibernate.cfg.xml
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/students</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/students</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
- 創建持久化類
import java.util.Date;
//學生類
public class Students {
private int sid;// 學號
private String sname;// 姓名
private String gender;// 性別
private Date birthday;// 出生日期
private String address;// 地址
public Students() {
}
public Students(int sid, String sname, String gender, Date birthday,
String address) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
- 創建對象-關係映射文件 Students.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>
<class name="Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENEDER" />
</property>
<property name="birthday" type="java.util.Data">
<column name="BIRTHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
- 將Students.hbm.xml加入hibernate.cfg.xml
<mapping resource="Students.hbm.xml" />
- 創建數據庫students 刷新表就會自動產生students表
- @Test:測試方法
- @Before:初始化方法
- @After:釋放資源
- 通過Hibernate API編寫訪問數據庫的代碼
- Configuration config = new Configuration().configure();//創建配置對象
- ServiceRegistry seviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();//創建服務註冊對象
- sessionFactory = config.buildSessionFactory(serviceRegistry);//創建會話工廠對象
- session = sessionFactory.openSession();//打開對話
package Test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.Students;
//測試類
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transation;
@Before
public void init() {
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transation = session.beginTransaction();
}
@After
public void destory() {
transation.commit();//提交事務
session.close();//關閉會話
sessionFactory.close();//關閉會話工廠
}
@Test
public void testSaveStudents() {
//生成學生對象
Students s = new Students(1, "小明", "男", new Date(), "中國");
session.save(s);//保存對象進入數據庫
}
}