天天看點

手把手教你配置hibernate

從今天開始,我将與大家分享SSH架構的配置過程,希望大家指點。

1、  建立Web項目導入hibernate的jar包,複制進WebRoot\WEB-INF\lib檔案夾下即可,這些包可以根據名稱在你下載下傳的hibernate檔案中找到。

手把手教你配置hibernate

2、  在SRC目錄下建立hibernate.cfg.xml配置檔案

a、選擇basic templates基礎xml模闆,名稱必須是hibernate.cfg.xml,打開如下圖所示

手把手教你配置hibernate

b、選擇DTD檔案,前提是必須導入DTD檔案模闆後才能找到

手把手教你配置hibernate

c、選擇select XML Catalogentry,選擇configuration DTD 3.0檔案

手把手教你配置hibernate

d、next之後點選finish即可

手把手教你配置hibernate

3、建立完hibernate配置檔案後寫配置資訊,關于資料庫相關的配置資訊

<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">
  		oracle.jdbc.driver.OracleDriver
  	</property>
  	<property name="connection.url">
  		jdbc:oracle:thin:@locathost:1521:orcl
  	</property>
  	<property name="connection.username">huan</property>
  	<property name="connection.password">orcl</property>
  	<property name="dialect">
  		org.hibernate.dialect.Oracle10gDialect
  	</property>
  	<property name="show_sql">true</property>
  	<property name="format_sql">true</property>
  </session-factory>
</hibernate-configuration>
           

4、對照資料庫的表寫實體類,寫實體類的時候要考慮兩個問題,一是放棄基本類型,使用包含類型,例如放棄int類型,而寫成Integer類型;二是明确寫出類與類之間的關系,有些關系雖然存在,但是用不到的話可以不寫,不是強制的,完全根據程式來判斷。

實體類

package com.cinema.entity;

import java.util.*;

public class FileType {
	private Integer typeId;
	private String typeName;
	private Set<FilmInfo> filmList = new HashSet<FilmInfo>();
	public Integer getTypeId() {
		return typeId;
	}
	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	public Set<FilmInfo> getFilmList() {
		return filmList;
	}
	public void setFilmList(Set<FilmInfo> filmList) {
		this.filmList = filmList;
	}
}
           

5、  寫實體類映射檔案,步驟與hibernate配置檔案類似,就是在幾個地方稍改一下。

a、檔案名是固定格式“實體類名.hbm.xml”

b、選擇映射DTD檔案

手把手教你配置hibernate

映射檔案

<?xml version="1.0" encoding="UTF-8"?>
<!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.cinema.entity.FileType" table="filetype">
		<id name="typeId" type="java.lang.Integer" column="typeid">
			<generator class="sequence">
				<param name="sequence">seq_filetype</param>
			</generator>
		</id>
		<property name="typeName" column="typename"/>
		<set name="filmList" table="filminfo">
			<key column="typeid"/>
			<one-to-many class="com.cinema.entity.FilmInfo"/>
		</set>
	</class>
</hibernate-mapping>
           

6、寫HibernateUtil類,這類負責打開資料庫

package com.cinema.util;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	
	static{
		try {
			sessionFactory = new Configuration().configure().buildSessionFactory();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			System.out.println("建立SessionFacttory錯誤:" + e);
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static Session getSession(){
		Session session = threadLocal.get();
		if(session == null){
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}
		return session;
	}
}
           

時間不早了,今天就寫到這裡。