【Hibernate】將物件儲存到資料庫表中
阿新 • • 發佈:2019-01-06
上一篇文章簡單介紹下了Hibernate的基本原理,這篇文章主要介紹下Hibernate的使用,將物件儲存到資料庫的
表中的流程。
一、搭建hibernate環境
1.將hibernate的jar包加入到工作環境中。
2.編寫Hibernate配置檔案,命名為:hibernate.cfg.xml
這裡以User表為例
<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
二、建立User實體類
get,set方法省略不寫啦
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
}
三、建立實體對映檔案
這裡的實體關係對映採用配置檔案的形式,還有一種是註解的形式,在這裡先不做介紹。
<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
四、編寫客戶端,將User表中的資料寫入到資料庫中
package com.bjpowernode.hibernate; import java.sql.Date; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; public class Client { public static void main(String[] args) { // 1.第一步:讀取hiber.cfg.xml檔案 Configuration cfg = new Configuration().configure(); // 2.第二步:建立sessionfactory SessionFactory factory = cfg.buildSessionFactory(); // 3.第三步:取得session, Session session = null; try { session = factory.openSession(); // 4.第四步:開啟事務 session.beginTransaction(); // 獲取當前日期 java.sql.Date currentDate = new java.sql.Date( System.currentTimeMillis()); // 例項化User類 User user = new User(); // 給User實體類賦值 user.setCreateTime(currentDate); user.setExpireTime(currentDate); user.setName("123123"); user.setPassword("123123"); // 儲存user物件 session.save(user); // 提交事務 session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); // 回滾事務 session.getTransaction().rollback(); } finally { if (session != null) { if (session.isOpen()) { // 關閉session session.close(); } } } } }
上面介紹了下Hibernate是怎樣儲存資料的,Hibernate還有一個功能是自動在資料庫中生成相對應的表,但是
資料庫得由我們自己手動建立
/**
* 將hbm生成ddl
* @author Administrator
*
*/
public class ExportDB {
public static void main(String[] args) {
//預設讀取hibernate.cfg.xml檔案
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//在資料庫中建立所對應的表,先讀取配置檔案的資訊,逐一建立表
export.create(true, true);
}
}
好啦,一個簡單的例項介紹了下hibernate將物件寫入 到資料庫中的具體操作,下篇文章將介紹Hibernate的
對映關係!