首先在接到一个项目,考虑到用SSH框架比较放心,决定从新开始整合一下SSH框架,好好巩固一下。这篇文章主要是针对初学者(跟我一样的,大牛们看看希望可以指正~~ =_=|||)、
首先我目前的开发环境是Windows10+Tomcat+maven+MySQL+Eclipse。
所谓SSH框架,就是整合了Spring+Struts+Hibernate的一个集成框架,从系统职责上分为四层:表示层、业务逻辑层、数据持久层和模块层。不清楚的可以去百度,这个就不多说了。
我们要搭建一个SSH框架,首先为了理解我们就先分别搭建,我们用别人东西,主要就是两点,1.导入jar包,2.配置相关的配置文件。我们一步一步来:
一、Struts2的配置
1>jar包:这是我的maven的pom.xml里边的依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1.1</version>
</dependency>
由于我们用的是MAVEN,MAVEN是有依赖的,所以当我们导入Struts2-core这个包时,相关必须的jar包也会被导入;如下图:
2>配置文件,(1) Struts2的配置文件为Struts2.xml。我们配置到web工程的resources中,这是拷贝进来的Struts2.xml文件(还未修改);
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 配置为开发者模式 -->
<constant name="struts.devMode" value="false" />
<!-- 将扩展名配置为action -->
<constant name="struts.action.extension" value="action"></constant>
<!-- 将主题配置为simple -->
<constant name="struts.ui.theme" value="simple"></constant>
<package name="default" namespace="/" extends="struts-default">
</package>
<!-- Add packages here -->
</struts>
(2)然后再web.xml中配置Struts2的核心的过滤器:
<!-- 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>
到此,Struts2的环境已经搭好。
二、Hibernate3配置
1>jar包:maven的pom.xml里边的依赖(如果用MYSQL,还需要单独导入一个c3p0的jar包和数据库驱动jar包)
2>配置文件:Hibernate的配置文件有两个,一个是主配置文件hibernate.cfg.xml和映射文件*.hbm.xml,我们首先配置主配置文件:
(1)hibernate.cfg.xml文件:
<!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="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.56.102:3306/excelword</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 其他配置 -->
<!-- 显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 自动建表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 导入映射文件*.hbm.xml -->
</session-factory>
</hibernate-configuration>
/*具体配置信息根据自己实际情况配置,在这里我们暂时不配置映射文件*/
三、Spring的配置:
1>jar包:maven的pom.xml里边的依赖(需要逐个导入)
2>.配置文件:applicationContext.xml(其中引号中需要根据具体包填写)
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package=""></context:component-scan>
</beans>
到此。三个环境都配置完成了;接下来我们开始进行整合
我们两两进行整合,首先是整合Spring+Struts2,首先我们要弄明白为什么要整合Spring和Struts2?
因为Spring是一个轻量级的框架,他最大的特点就是提供了AOP和IOC两个特性功能,而我们整合Spring和Struts就利用到了Spring的IOC功能,也就是用Spring容器来管理Struts的action对象。
需要有两个步骤:
1>在web.xml中配置Spring用于初始化容器对象的监听器
<!-- 配置Spring用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
classpath根据自己的实际情况填写
2>导入一个整合的jar包:Struts2-Spring-plugin 2.3.1.1(版本自定)
这样,Struts与Spring就算整合完成。需要注意:在整合后,Struts的配置文件中,action标签的class由原来的包的全限定名字可以改为bean的名称。
接下来我们整合Spring和hibernate,首先我们要知道Spring和hibernate整合的原因?
上边我们说到了Spring的两个功能,而Spring和hibernate整合就用到了Spring的AOP事务管理。也就是让Spring来管理sessionfactory实例和声明式事务管理配置
我们在整合Spring和hibernate的时候,需要两个步骤:
1>首先我们需要在Spring的配置文件中配置sessionfactory的bean进行管理,并且可以吧原来在hibernate.cfg.xml中的链接信息写进applicationContext.xml中,由bean管理
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="cn.excel.upanddown"/>
<!-- 配置sessionfactory -->
<bean id="sessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0连接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="jdbcUrl" value=""/>
<property name="driverClass" value=""/>
<property name="user" value=""/>
<property name="password" value=""/>
<!-- 其他配置信息 -->
<property name="initialPoolSize" value="3"/>
<property name="minPoolSize" value="3"/>
<property name="maxPoolSize" value="5"/>
<property name="acquireIncrement" value="3"/>
<property name="maxStatements" value="8"/>
<property name="maxStatementsPerConnection" value="5"/>
<property name="maxIdleTime" value="1800"/>
</bean>
</property>
</bean>
<!-- 声明式事务管理(采用注解配置) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionfactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
2>将原来hibernate.cfg.xml中的连接信息删掉
这样基本的搭建就完成了,可以在Tomcat上跑跑。写一个测试的包去测试一下。