從今天開始,我將與大家分享SSH框架的配置過程,希望大家指點。
1、 創建Web項目導入hibernate的jar包,複製進WebRoot\WEB-INF\lib文件夾下即可,這些包可以根據名稱在你下載的hibernate文件中找到。
2、 在SRC目錄下創建hibernate.cfg.xml配置文件
a、選擇basic templates基礎xml模板,名稱必須是hibernate.cfg.xml,打開如下圖所示b、選擇DTD文件,前提是必須導入DTD文件模板後才能找到
c、選擇select XML Catalogentry,選擇configuration DTD 3.0文件
d、next之後點擊finish即可
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文件
映射文件
<?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;
}
}
時間不早了,今天就寫到這裏。