hebarnate第一個例子
一、Hibernate概述
(一)什麼是Hibernate?
Hibernate核心內容是ORM(關係物件模型)。可以將物件自動的生成資料庫中的資訊,使得開發更加的面向物件。這樣作為程式設計師就可以使用面向物件的思想來操作資料庫,而不用關心繁瑣的JDBC。所以,Hibernate處於三層架構中的D層(持久層)。
(二)使用Hibernate的優點
1、Hibernate可以使用在java的任何專案中,不一定非要使用在java web專案中。因為Hibernate不需要類似於tomact這些容器的支援,可以直接通過一個main方法進行測試。
2、通過下面的例項,可以發現使用Hibernate可以大大減少程式碼量。
3、由於使用了Hibernate,程式碼中不涉及具體的JDBC語句,所以就方便了程式碼的可移植性。
二、Hibernate開發的環境搭建
(一)Hibernate的環境搭建非常簡單,只需要引入Hibernate核心包(單擊下載)以及Hibernate依賴包(單擊下載)即可。
(二)加入資料庫驅動。下面的例子中主要是採用Mysql資料庫來演示的,所以在這裡引入MysqL的JDBC驅動(點選下載)。
(三)提供核心配置檔案hibernate.cfg.xml檔案(在src資料夾下即可)。其中的配置如下(針對mysql)
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory > <!-- mysql資料庫驅動 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- mysql資料庫名稱 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property> <!-- 資料庫的登陸使用者名稱 --> <property name="hibernate.connection.username">root</property> <!-- 資料庫的登陸密碼 --> <property name="hibernate.connection.password">root</property> <!-- 方言:為每一種資料庫提供介面卡,方便轉換 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory> </hibernate-configuration>
三、HIbernate第一個例項
該例項的目錄結構如下
說明:最後一個HIBERNATE3裡面包含了所有的需要引用的jar包
1、新建一個普通的java專案,按照上面的步驟引入相關的jar包和配置檔案
2、建立User實體類
import java.util.Date; public class User { private String id; private String username; private String password; private Date createTime; private Date expireTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String userName) { this.username = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } }
2、提供User.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="com.example.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
其中的property標籤是將要生成是資料庫表中的欄位,在這裡不用關心各個欄位是什麼型別的。因為Hibernate會根據上面的實體類中屬性的型別來決定將來表中欄位的型別
3、配置hibernate.cfg.xml檔案
<hibernate-configuration>
<session-factory >
<!-- mysql資料庫驅動 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql資料庫名稱 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<!-- 資料庫的登陸使用者名稱 -->
<property name="hibernate.connection.username">root</property>
<!-- 資料庫的登陸密碼 -->
<property name="hibernate.connection.password">root</property>
<!-- 方言:為每一種資料庫提供介面卡,方便轉換 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/example/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
注意:必須是“/”而不能是“.”。
4、生成表:編寫工具類ExoprtDB.java,將hbm生成ddl
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 將hbm生成ddl
* @author BCH
*
*/
public class ExoprtDB {
public static void main(String[] args) {
//預設讀取hibernate.cfg.xml檔案
Configuration cfr = new Configuration().configure();
SchemaExport export = new SchemaExport(cfr);
export.create(true, true);
}
}
到這裡就可以生成User表了,但是如果直接執行ExoprtDB.java檔案是不能生成User表的。因為在mysql資料中還沒有建立資料庫Hibernate-first。所以在mysql控制檯中通過create database hibernate-first; use hibernate-first;之後再執行ExoprtDB.java檔案就可以生成表了。
5、向表中新增資料
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) {
//讀取配置檔案
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try{
session = factory.openSession();
//開啟事務
session.beginTransaction();
User user = new User();
user.setUsername("使用者名稱");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
session.save(user);
//提交事務
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
//回滾事務
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//關閉session
session.close();
}
}
}
}
}
執行該java檔案就可以完成向表中增加資料了,效果如下
(四)總結
通過上面的程式碼我們可以看出,在程式碼中沒有涉及到任何有關JDBC的程式碼,作為開發人員只需要寫好相應的實體類,然後通過配置就可以實現了表的建立以及向表中實現資料的插入。
在程式碼中有許多Hibernate的核心物件,例如Configuration、SessionFactory、Session等等。這些內容將在以後介紹。
作者:aboy123
來源:CSDN
原文:https://blog.csdn.net/aboy123/article/details/10085635
版權宣告:本文為博主原創文章,轉載請附上博文連結!