从今天开始,我将与大家分享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;
}
}
时间不早了,今天就写到这里。