1 无障碍整合
1.1 创建项目,引入jar包
1.1.1 Struts的jar包
- 在struts-2.3.24\apps\struts2-blank\WEB-INF\lib\*.jar路径下的包,除了日志包和javassist-3.11.0.GA.jar包可以不引以外,其他的都需要引入;
- 此外Struts2中还有一些其他的包:
- struts2-convention-plugin-2.3.24.jar ----Struts2的注解开发包。(Struts-2.3.24/lib)
- struts2-json-plugin-2.3.24.jar ----Struts2的整合AJAX的开发包。
- struts2-spring-plugin-2.3.24.jar(这个要引) ----Struts2的整合Spring的(插件)开发包。
struts2-spring-plugin-2.3.24.jar的struts-plugin.xml文件中有如下配置
那么这个有什么含义呢?
我们先在Struts2中的default.properties中找到struts.objectFactory默认常量是关闭的
这个常量的关闭导致除①以外三个常量无法使用
尤其是struts.objectFactory.spring.autoWire = name,这个常量的作用就是struts对象按名称自动注入spring
1.1.2 Hibernate的jar包
- Hibernate的开发的必须的包 : hibernate-release-5.0.7.Final\lib\required\*.jar
注意:Struts2和Hibernate都引入了一个相同的jar包(javassist包)。删除一个******
- mysql驱动:mysql-connector-java-5.1.7-bin.jar
- 日志记录(Slfj与log4j整合的包很重要)
- 使用C3P0连接池(下面两个是Hibernate整合C3P0的包,第一个可以现在引,也可以在引入Spring的包的时候再引):
1.1.3 Spring的jar包
- IOC的开发(log4j可以不要,但是第一个logging必须要)
- AOP的开发
- JDBC模板的开发(spring整合jdbc,spring的事务)
- 整合web项目的开发
- 整合单元测试的开发
- 整合hibernate的开发
1.2 引入配置文件
1.2.1 web.xml
在web.xml中需要配置struts2的核心过滤器、Spring的核心监听器以及Spring的配置文件的路径
<!-- 配置struts核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载Spring的配置文件的路径,默认加载的是/WEB-INF/applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
1.2.2 Hibernate.cfg.xml
hibernate的核心配置文件不用配置与线程绑定的session以及事务的隔离级别,由Spring来统一管理事务
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">NULIFENDOU520</property>
<!-- 配置数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 下面的为可选配置 -->
<!-- 是否打印sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置C3P0连接池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,
如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
<!-- 映射orm元数据,即将与数据库中的表相对应的对象的配置文件引入进来 -->
<mapping resource ="com/itykd/domain/Customer.hbm.xml"/>
<mapping resource ="com/itykd/domain/LinkMan.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.2.3 mapper.hbm.xml
配置POJO类与数据表的映射
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-20 16:04:05 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<!--将实体的数据与表的数据相关连,即orm的一个联系 -->
<class name="com.itykd.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<!-- 主键生成策略-->
<generator class="native"/>
</id>
<!-- 除了主键之外其他属性的对应关系 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<set name="linkMans">
<key>
<column name="lkm_cust_id"/>
</key>
<one-to-many class="com.itykd.domain.LinkMan" />
</set>
</class>
</hibernate-mapping>
1.2.4 struts.xml
配置相应的action类,action标签的class属性使用的一般是类的全路径,但是下面使用的是在applicationContext.xml中配置好的action类的id,也就是说Action是交给Spring去管理的。这样的好处是当我们需要对Action进行增强的时候,就可以利用Spring的AOP进行增强,比较方便
<?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="SSH" extends="struts-default" namespace="/">
<action name="customer_*" class="customerAction" method="{1}">
</action>
</package>
</struts>
1.2.5 applicationContext.xml
applicationContext.xml中主要配置了由Spring管理的Action、Service、Dao、session工厂、平台事务管理器,注解事务和注解属性的配置、Hibernate配置文件的引入等
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Spring整合Hibernate -->
<!-- 引入Hibernate的配置的信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入Hibernate的配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!--配置平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 开启注解属性 -->
<context:annotation-config />
<bean id="customerAction" class="com.itykd.web.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>
<bean id="customerService" class="com.itykd.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<bean id ="customerDao" class="com.itykd.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
配置好如下所述的配置文件并写好相应的类,一个基本的SSH环境就搭建完毕了。下面介绍第二种:不带Hibernate配置文件的
2 将hibernate的配置交给Spring管理
jar包的引入、web.xml、struts.xml这三个都不需要改变
2.1 删除hibernate的核心配置文件和映射文件
删除hibernate的这两个文件,意味着Spring中的配置文件中要管理hibernate相应的配置(比如连接池,方言等)。具体的配置如下:
2.2 Spring管理hibernate的配置文件
- 首先创建一个属性文件(jdbc.properties)
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///SSH
jdbc.username=root
jdbc.password=NULIFENDOU520
- spring管理hibernate的配置(applicationiContext.xml)
这里需要将jdbc.properties引入配置文件,然后配置C3P0连接池、Hibernate的属性,如下所示
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Spring整合Hibernate -->
<!-- 引入Hibernate的配置的信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernante其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 设置映射文件 -->
<property name="mappingResources">
<list>
<value>com/itykd/domain/Customer.hbm.xml</value>
<value>com/itykd/domain/LinkMan.hbm.xml</value>
</list>
</property>
</bean>
这样就完成了不带Hibernate配置文件的SSH的整合