クイックハンド

6286 ワード

以前はHibernateについてよく知っていて、Hibernateを使うメリットを知っていましたが、Hibernateを素早く使う方法を説明します.
環境構築:
1.Hibernateをダウンロードし、解凍する(ダウンロードアドレス:http://www.hibernate.org/downloads)
2.新しいJavaプロジェクトの作成
3.関連jarパッケージの導入
  Hibernate_Home/lib/*.jar
  Hibernate_Home/hibernate3.jar
データベースドライバの追加(mysqlの例)
 
注意:Hibernate_Homeはあなたが解凍したHibernateのディレクトリです
ここのデータベースはMysqlとして駆動されています.Home/src/lib/jdbc2_0-stdext.jar
 
4.hibernateを提供する.cfg.xmlファイル、基本的な構成を完了
 
<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">dandan</property>
		<!--            -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!--       SQL  ,      ,       true -->
		<property name="hibernate.show_sql">true</property> 
		 
	</session-factory>
</hibernate-configuration>

具体的な実装:
5.関連実装コードの作成
エンティティークラスjava
  
package com.dan.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;
	}	
}

エンティティークラスマッピングファイルUser.hbm.xml
 
<hibernate-mapping>
	<class name="com.dan.hibernate.User">
		<id name="id">
		    <!--       ,     uuid,        -->
			<generator class="uuid" />
		</id>
		<property name="name" />
		<property name="password" />
		<property name="createTime" />
		<property name="expireTime" />
	</class>
</hibernate-mapping>

ユーザーをhbm.xmlファイルはhibernateに追加されます.cfg.xmlファイル
  
<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">dandan</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property> 
		 
		<!--           -->
		<mapping resource="com/dan/hibernate/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>  
       ExportDB.java, hbm  ddl(      )
   
     
package com.dan.hibernate;

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

public class ExportDB {

	public static void main(String[] args){
		
		//    hibernate.cfg.xml  
		Configuration cfg = new Configuration().configure();
		
		// ddl         ,   ddl  
		SchemaExport export = new SchemaExport(cfg);
		export.create(true, true);
	}
}
  
        Client,       mysql
     
package com.dan.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();
				}
			}
		}
	}
}
   

6. mysql , “hibernate”

  :create  database hibernate_first;

 

7.

  ExportDB

  Client

 

    mysql

    :use hibernate_first;

    :show tables;

    :select * from user;

      Hibernate_ 1

 

, !

:zhaodandan19910306 2012-2-25 12:56:40
:3 :0