目录
1、项目架构
1.1 新建一个Exam项目
项目类型为Dynamic Web Project,其中Dynamic Web Version为2.5;
1.2 导入相关的配置文件和jar包
配置文件主要有(放在src目录下):
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url"><![CDATA[jdbc:mysql://localhost:3306/db_exam?useUnicode=true&characterEncoding=utf8]]></property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 把session绑定到当前线程中 -->
<property name="current_session_context_class">thread</property>
<!-- 控制台显示SQL -->
<property name="show_sql">true</property>
<!-- 自动表更新 -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="studentInfo" namespace="/" extends="struts-default">
</package>
</struts>
其中struts还必须修改web.xml配置文件;
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Exam</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
相关的jar包有(放在Web Content下的lib文件夹下面):
这些都是hibernate和struts的核心jar包,还有mysql的驱动包等等;
2.建立工具包
建立工具包com.java.util;
并建立HibernateUtil工具类:
package com.java.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
//获取SessionFactory的方法
private static SessionFactory buildSessionFactory(){
//实例化配置文件
Configuration configuration=new Configuration().configure();
//实例化服务注册
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
//获取session工厂
return configuration.buildSessionFactory(serviceRegistry);
}
//设置静态字段
private static final SessionFactory sessionFactory=buildSessionFactory();
//获取当前的单例SessionFactory,提供对外的接口
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
//测试是否成功
public static void main(String[] args) {
HibernateUtil.getSessionFactory();
}
}
这里我们提前要在数据库里建好一个db_exam数据库 ;
我们可以测试运行一下:
这样就表示HibernateUtil运行成功,环境搭建成功。