下面對 SSH 框架做一個整合,所用的三大框架的版本號 Struts2.3.x,Spring4.x,hibernate5.x。
1.回顧 SSH 框架知識點
1.1 Hibernate 框架
- Hibernate 的核心配置文件:數據庫信息,Hibernate信息,映射配置。如果單純使用 Hibernate 框架,核心配置文件的名稱(hibernate.cfg.xml)和位置(src 下面)都是固定的。但是在和 Spring 整合的時候,Hibernate 的核心配置文件名稱和位置是沒有固定要求的。
- Hibernate 的映射配置文件:orm思想,對象關系映射。實體類和數據表映射關系——使用orm思想。
在 Spring 框架對 Hibernate 框架進行封裝時,使用 HibernateTemplate 類。
1.2 Struts2 框架
- Action 操作:Action 創建的三種方式,一般使用繼承 ActionSupport 類來創建 Action類。配置 創建 struts.xml 配置文件來配置 Action 的訪問路徑,配置文件的名稱和位置(src下面)都是固定的。配置Action 類中的多個方法使用通配符的方式。在使用 Action 類獲取表單提交的數據時,一般使用 ServletActionContext 類來獲取,還有屬性封裝,模型驅動,表達式封裝。在 Action 中操作域對象時使用 ServletActionContext 來獲取。另外還有Struts2 的過濾器配置
- 值棧:向值棧中放數據,set 方法和 push 方法,定義變量,生成 get 方法。從值棧中獲取數據,在jsp中使用 Struts2 標簽加上 OGNL 表達式來獲取。標簽
<s:propertity>
,<s:iterator>
。 - 攔截器:AOP 思想和責任鏈模式,自定義攔截器繼承 MethodFilterInterceptor 類,重寫父類里面的方法,配置攔截器和 Action 關聯。
1.3 Spring 框架
- Spring 核心配置文件:名稱和位置沒有固定的要求,在 Spring 核心配置文件中引入 scheme 約束。
- 創建對象:xml 配置方式,
<bean id="" class="" scope=""/>
;注解方式,四個注解,@Component,@Repostiry,@Service,@Controller。 - 屬性注入:xml 配置方式,
<property name="" ref="">
;注解方式,兩個注解,@AutoWired,@Resource。 - 使用 ServletContext 對象和監聽器,實現在服務器啟動時就加載 Spring 的配置文件創建對象,配置 Spring 的監聽器,指定 Spring 配置文件的位置。
- JdbcTemplate
- Spring 的事務管理:xml 方式和注解方式。
2.SSH框架整合
2.1 SSH 框架整合的思想
Struts2 負責和界面數據交互,路徑跳轉,攔截請求,調用 Service 層中的方法。Spring 負責另外兩個框架中的對象創建,實現業務邏輯,調用 Dao 層中的數據操作的方法。Hibernate 負責和數據庫交互,增刪改查等等。
2.2 SSH 框架整合準備
創建一個文件夾,將之前三大框架的用到的 jar 包放進去,另外還要加入三個 jar 包。一個是整合整個 java web 項目的 spring-web.jar
,一個是整合struts2的 struts2-spring-plugin.jar
,一個是整合持久層框架的 spring-orm.jar
。
2.3 Spring 整合開發
下面通過添加學生的例子來說明。
實體類
package cc.wenshixin.entity;
public class Student {
private int id;
private String name;
private String sex;
//get和set方法
實體類的映射配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cc.wenshixin.entity.Student" table="student">
<id name="id" type="int">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" />
</property>
</class>
</hibernate-mapping>
Hibernate 的核心配置文件
Hibernate 的數據庫配置部分放到 c3p0 連接池的配置中。
<?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>
<!-- 配置hibernate -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 引入映射文件 -->
<mapping resource="cc/wenshixin/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
配置文件
首先是整個 web 項目的 web.xml 文件的配置,除了在 struts2 的攔截器,還要配置 spring 核心配置文件的位置,classpath
寫spring核心配置文件的位置,一般都是直接放在 src 下,另外配置監聽器,來在服務器啟動時,就創建 Hibernate 的相關對象,解決第一次做數據庫操作比較慢的問題。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssh01</display-name>
<!-- spring的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
<!-- struts2攔截器的配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 監聽器的配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>
struts2 核心配置文件
<?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="demo" extends="struts-default" namespace="/">
<action name="studentAction" class="studentAction"></action>
</package>
</struts>
spring 的核心配置文件
Spring 框架中操作 JDBC 的是 JdbcTemplate,但對于其他持久層也封裝了相應的類,操作 Hibernate 是 HibernateTemplate 類,但也需要注入 sessionFactory
屬性,根據 c3p0 連接池來配置 sessionFactory 對象,另外還需要配置事務管理。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置數據庫信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入屬性值 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?useSSL=false"></property>
<property name="user" value=""></property>
<property name="password" value=""></property>
</bean>
<!-- 配置sessionFactory創建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 制定數據庫的信息 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定使用的hibernate核心配置文件的位置 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 注入sessionFacory對象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 開啟事務注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置hibernateTemplate對象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置三層結構對象 -->
<bean id="studentAction" class="cc.wenshixin.action.StudentAction"></bean>
<bean id="studentService" class="cc.wenshixin.service.StudentService"></bean>
<bean id="studentDao" class="cc.wenshixin.dao.StudentDao"></bean>
<!-- 開啟注解掃描 -->
<context:component-scan base-package="cc.wenshixin"></context:component-scan>
</beans>
Dao 類
package cc.wenshixin.dao;
import javax.annotation.Resource;
import org.springframework.orm.hibernate5.HibernateTemplate;
import cc.wenshixin.entity.Student;
public class StudentDao{
@Resource(name="hibernateTemplate")
private HibernateTemplate hibernateTemplate;
public void add() {
Student student = new Student();
student.setName("james");
student.setSex("男");
hibernateTemplate.save(student);
}
}
Service 類
package cc.wenshixin.service;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import cc.wenshixin.dao.StudentDao;
@Transactional
public class StudentService {
@Resource(name="studentDao")
private StudentDao studentDao;
public void add()
{
System.out.println("service...");
studentDao.add();
}
}
Action 類
package cc.wenshixin.action;
import javax.annotation.Resource;
import com.opensymphony.xwork2.ActionSupport;
import cc.wenshixin.service.StudentService;
public class StudentAction extends ActionSupport{
@Resource(name="studentService")
private StudentService studentService;
@Override
public String execute() throws Exception {
System.out.println("action...");
studentService.add();
return NONE;
}
}
訪問 http://localhost:8080/ssh01/studentAction.action。
2.3 SSH 分模塊開發
在開發中,通常要進行分模塊開放,也就是把核心配置文件中的內容拆開,在核心配置文件中包含其他的配置文件,減少對核心配置文件的改動,將一個項目分成小的模塊,多人一起開發。
下面通過一個表單提交學生信息的例子來說明。
實體類
package cc.wenshixin.entity;
public class Student {
private String id; //學號
private String name; //姓名
private String banji; //班級
//get和set方法
實體類的映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cc.wenshixin.entity.Student" table="student">
<id name="id" type="java.lang.String">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="banji" type="java.lang.String">
<column name="banji" />
</property>
</class>
</hibernate-mapping>
整個 web 項目的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssh02</display-name>
<!-- Spring核心配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 過濾器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>
Dao 類
創建一個 Dao 類的接口,其他 Dao 類實現這個類中的方法。
package cc.wenshixin.dao;
public interface AbstractDao {
public void add(Object object);
}
package cc.wenshixin.dao;
import javax.annotation.Resource;
import org.springframework.orm.hibernate5.HibernateTemplate;
import cc.wenshixin.entity.Student;
public class StudentDao implements AbstractDao{
@Resource(name="hibernateTemplate")
private HibernateTemplate hibernateTemplate;
@Override
public void add(Object object) {
System.out.println("dao。。。");
hibernateTemplate.save((Student)object);
}
}
Service 類
package cc.wenshixin.service;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import cc.wenshixin.dao.StudentDao;
import cc.wenshixin.entity.Student;
@Transactional
public class StudentService {
@Resource(name="studentDao")
private StudentDao studentDao;
public void add(Student student)
{
System.out.println("service。。。");
studentDao.add(student);
}
}
Action 類
package cc.wenshixin.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cc.wenshixin.entity.Student;
import cc.wenshixin.service.StudentService;
public class StudentAction extends ActionSupport {
@Resource(name="studentService")
private StudentService studentService;
private HttpServletRequest request = ServletActionContext.getRequest();
public String add() throws Exception
{
String id = request.getParameter("id");
String name = request.getParameter("name");
String banji = request.getParameter("banji");
Student student = new Student();
student.setId(id);
student.setName(name);
student.setBanji(banji);
System.out.println("action。。。");
studentService.add(student);
return NONE;
}
}
單獨創建一個包來存放非核心的配置文件,spring 和 struts2 的核心配置文件仍舊放在 src 目錄下,另外在 spring 中配置 hibernate 的選項和引入映射文件,可以不再單獨寫 Hibernate的配置文件。
student-spring.xml
放 student 相關的 Action 類,Service 類,DAO 類。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置對象 -->
<bean id="studentAction" class="cc.wenshixin.action.StudentAction" scope="prototype"></bean>
<bean id="studentService" class="cc.wenshixin.service.StudentService"></bean>
<bean id="studentDao" class="cc.wenshixin.dao.StudentDao"></bean>
</beans>
student-struts.xml
放 Action 類中的方法的配置
<?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="student" extends="struts-default" namespace="/">
<action name="addStudent" class="studentAction" method="add"></action>
</package>
</struts>
Spring 核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置數據庫信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入屬性值 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?useSSL=false"></property>
<property name="user" value=""></property>
<property name="password" value=""></property>
</bean>
<!-- 配置sessionFactory創建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 指定數據庫的信息 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate選項 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
</props>
</property>
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>cc/wenshixin/entity/Student.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 注入sessionFactory對象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 開啟事務注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置對象 -->
<bean id="studentAction" class="cc.wenshixin.action.StudentAction"></bean>
<bean id="studentService" class="cc.wenshixin.service.StudentService"></bean>
<bean id="studentDao" class="cc.wenshixin.dao.StudentDao"></bean>
<!-- 開啟屬性注入注解 -->
<context:component-scan base-package="cc.wenshixin"></context:component-scan>
<!-- 配置hibernateTemplate對象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 引入對象配置文件 -->
<import resource="classpath:cc/wenshixin/config/student-spring.xml"/>
</beans>
Struts 核心配置文件
<?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="default" extends="struts-default" namespace="/"></package>
<include file="cc/wenshixin/config/student-struts.xml"></include>
</struts>
整個項目的結構
log4j的配置文件不是必須的,但建議使用,便于查找錯誤。
SSH 框架之旅到這里才剛剛開始。