1. 程式人生 > >Hibernate框架的搭建和第一個簡單的例項

Hibernate框架的搭建和第一個簡單的例項

Hibernate是一個支援對JDBC進行封裝的框架,實現了對底層資料庫訪問的封裝。非常適合使用和開發。首先需要下

載Hibernate,可以在這個網站下載最新包。http://www.hibernate.org/然後開啟他的目錄結構,將lib目錄下的required目

錄下的包全部匯入到工程中去,這個是hibernate執行所必須的最少的包。

然後寫一個Bean,將需要儲存到資料庫中的變數封裝成Bean。為了讓Hibernate識別這個bean,需要一個配置文

件,這裡起名叫User.hbm.xml。先看一下User的程式碼和User.hbm.xml的程式碼

package com.bird.domain;

import java.util.Date;

public class User {

	private int id;
	private String name;
	private Date birthday;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}

<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bird.domain">
   	
   	<class name="User">
   		<id name="id">
   			<generator class="native"/>
   		</id>
   		
   	<property name="name"/>
   	<property name="birthday"/>
   	
   	</class>
   	
</hibernate-mapping>

然後需要一個Hibernate的配置檔案,這個檔案的例子可以再Hibenate解壓目錄的project裡面的ect目錄裡面找到。更加

詳細的配置選項和要求可以參考hibernate.properties.template這個檔案.

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">mysql</property>
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<mapping resource="com/bird/domain/User.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>這句話的意思是指定你使用的數

據庫的方言.

<property name="hibernate.hbm2ddl.auto">update</property>這句話的意思是自動建立或者更改資料庫裡面的表或

者表的內容結構

<mapping resource="com/bird/domain/User.hbm.xml"/>這句話的意思是要求裝載這個類對映檔案

下面就可以執行這個了,記住,別忘了匯入Mysql的Connection的Jar包。

package com.bird.hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.bird.domain.User;

public class Base {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Configuration cfg = new Configuration();
		cfg.configure();
		
		@SuppressWarnings("deprecation")
		SessionFactory sf = cfg.buildSessionFactory();
		
		Session s = sf.openSession();
		
		Transaction tx = s.beginTransaction();
		User use = new User();
		use.setBirthday(new Date());
		use.setName("bird");
		
		s.save(use);
		tx.commit();
		s.close();
		
	}

}

執行結果如下
2012-2-28 12:12:38 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2012-2-28 12:12:38 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.1.Final}
2012-2-28 12:12:38 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2012-2-28 12:12:38 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2012-2-28 12:12:38 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2012-2-28 12:12:38 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2012-2-28 12:12:38 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/bird/domain/User.hbm.xml
2012-2-28 12:12:38 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///test]
2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2012-2-28 12:12:39 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2012-2-28 12:12:39 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2012-2-28 12:12:39 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: test.user
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, birthday, name]
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete

這樣就可以了