4.8 网站页面关系图
由于本网站采用了Hibernate开发架构实现了数据持久层处理的功能,因此要给出Hibernate相关的配置文件及映射文件的具体源代码及其功能.对应Hibernate的配置文件和映射文件要放置在项目的src目录中, ,而用于管理session以及封装了数据库操作相关方法的类则与其他类一样,放到相应的包中, 本项目的相对路径为com.chjx.
在该文件中主要是进行SessionFactory配置(可以理解成建立与数据库之间连接的配置)在Hibernate中,既可以用XML文件,也可以使用属性文件来配置,而本系统则使用的是XML.该文件的代码如下:其中粗体的部分数据库的类型,服务器,端口号,数据库名称等相关配置.
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">conn_SQL</property>
<property name="connection.url">jdbc:microsoft:sqlserver://L4:1433;SelectMethod=cursor;DatabaseName=chjx</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="com/chjx/Userinfo.hbm.xml"></mapping>
<mapping resource="com/chjx/Invite.hbm.xml"></mapping>
<mapping resource="com/chjx/Infoback.hbm.xml" />
<mapping resource="com/chjx/Orders.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
该映射文件是Hibernate架构中最基本也是最重要的一个配置文件,用于声明Hibernate中实现实体类的属性和数据库队形数据表字段之间的映射关系.这里仅给出留言表(infoback)对应的配置文件infoback.hbm.xml的代码,其他表与之类似.
<?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" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat May 20 21:20:37 CST 2006 -->
<hibernate-mapping package="com.chjx">
<class name="Infoback" table="infoback">
<id name="id" column="id" type="java.lang.Integer">
<generator class="identity"/>
</id>
<property name="name" column="name" type="java.lang.String" not-null="true" />
<property name="who" column="who" type="java.lang.String" not-null="true" />
<property name="compname" column="compname" type="java.lang.String" not-null="true" />
<property name="tel" column="tel" type="java.lang.String" not-null="true" />
<property name="fax" column="fax" type="java.lang.String" not-null="true" />
<property name="email" column="email" type="java.lang.String" not-null="true" />
<property name="title" column="title" type="java.lang.String" not-null="true" />
<property name="content" column="content" type="java.lang.String" not-null="true" />
<property name="bktime" column="bktime" type="java.lang.String" not-null="true" />
<property name="reconfirm" column="reconfirm" type="java.lang.String" not-null="true" />
<property name="back" column="back" type="java.lang.String" />
</class>
</hibernate-mapping>
该类用于管理session的生成和关闭.其中声明管理session的类基本上符合在传统Java Web开发中的JDBC的Connection对象的概念,在Hibernate中,该session帮助实现和数据库之间的交互.在Hibernate的开发过程中,session类声明的格式基本上是固定的,在项目中新建一个类,该类应该放置在项目文件夹下的src对应的包中,本项目的相对路径为com.chjx.文件名为HibernateSessionFactory.java.
package com.chjx;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}
}
该类中封装了借助Hibernate机你姓女冠数据库相关操作的方法. 这里也仅给出留言表(infoback)对应的配置文件Infoback.java的代码,其他表与之类似.
package com.chjx;
import java.io.Serializable;
public class Infoback
extends AbstractInfoback
<< 上一页 [11] [12] [13] [14] 下一页