一、Hibernate概述
(一)什麼是Hibernate?
Hibernate核心內容是ORM(關係對象模型)。可以將對象自動的生成數據庫中的信息,使得開發更加的面向對象。這樣作爲程序員就可以使用面向對象的思想來操作數據庫,而不用關心繁瑣的JDBC。所以,Hibernate處於三層架構中的D層(持久層)。
(二)使用Hibernate的優點
1、Hibernate可以使用在java的任何項目中,不一定非要使用在java web項目中。因爲Hibernate不需要類似於tomact這些容器的支持,可以直接通過一個main方法進行測試。
2、通過下面的實例,可以發現使用Hibernate可以大大減少代碼量。
3、由於使用了Hibernate,代碼中不涉及具體的JDBC語句,所以就方便了代碼的可移植性。
二、Hibernate開發的環境搭建
<!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第一個實例
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;
}
}
<?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會根據上面的實體類中屬性的類型來決定將來表中字段的類型<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>
注意:必須是“/”而不能是“.”。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文件就可以生成表了。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();
}
}
}
}
}