table of Contents
1.2 import the relevant profile and jar package
1. Project architecture
1.1 Create a new project Exam
Project type Dynamic Web Project, which Dynamic Web Version 2.5;
1.2 import the relevant profile and jar package
There are configuration files (in the src directory):
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>
Web.xml struts which must also modify the configuration file;
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>
Related jar package there (on the Web Content in the lib folder below):
These are the core of struts and hibernate jar package, as well as mysql driver package, and so on;
2. Build Kit
Establish Kit com.java.util ;
And the establishment of HibernateUtil tools:
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();
}
}
Here we want to advance db_exam built a database in the database;
We can test run it:
This represents HibernateUtil run successfully, environment to build success.