1. 程式人生 > >Hibernate——以面向物件的思維操作關係資料庫(一)

Hibernate——以面向物件的思維操作關係資料庫(一)

Hibernate是一個輕量級的ORM開源框架。Hibernate解脫了面對繁瑣的JDBC的開發者,使得變成更加面向物件化,將之前的連線語句,增刪改查等語句進行了很好的封裝,使得業務邏輯等的編寫更加的容易;使移植資料庫也變得十分的容易,即根據不同的資料庫配置configuration.cfg.xml就可以輕鬆改變資料庫;下面以一個程式碼段稍作解釋:

一 :每個相對應的實體都會有相對應的對映檔案,對映檔案的作用主要是維護實體間的關係。

實體類User:

package com.bjpowernode.hibernate;

import java.util.Date;

public class User {

	private String id;
	
	private String name;
	
	private String password;
	
	private Date createTime;
	
	private Date expireTime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	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.bjpowernode.hibernate.User">
		<id name="id">
			<generator class="uuid"/>
		</id>
		<property name="name"/>
		<property name="password"/>
		<property name="createTime"/>
		<property name="expireTime"/>
	</class>
</hibernate-mapping>

將對映檔案生成dll檔案:

package com.bjpowernode.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * 將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);
	}
}


二 :configuration.cfg.xml中配置基本資訊,並將對映檔案資訊配置到configuration.cfg.xml

configuration.cfg.xml配置檔案:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<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">bjpowernode</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>

三:建立客戶端類Client,新增使用者資料到資料庫

package com.bjpowernode.hibernate;

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) {
		
		//讀取hibernate.cfg.xml檔案
		Configuration cfg = new Configuration().configure();
		
		//建立SessionFactory
		SessionFactory factory = cfg.buildSessionFactory();
		
		//取得session
		Session session = null;
		try {
			session = factory.openSession();
			//開啟事務
			session.beginTransaction();
			User user = new User();
			user.setName("張三");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			
			//儲存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();
				}
			}
		}
	}
}


總結:上面只是一個小例子,但是從小例子中我們依稀可以感受到那種封裝給我們帶來的便捷了。