SSH整合

1、导入依赖jar包:

下载地址

1.1 hibernate相关(5.2.12.Final)
hibernate-core
hibernate-c3p0(数据库连接池)
hibernate-ehcache
mysql-connector-java(5.1.44)

1.2 spring相关(5.0.1.RELEASE)
spring-context
spring-orm
spring-web
spring-aspects

1.3 struts2相关(2.5.13)
struts2-core
struts2-spring-plugin

1.4 log配置
1.4.1 log4j(1.X版)
不建议使用

1.4.2 log4j2(2.9.1)
log4j-core
log4j-api
log4j-web
不建议使用

1.4.3 log4j2 + slf4j(使用ehcache开启hibernate二级缓存必须使用第二种方式配置日志)

1.5 other
junit(4.12)
javax.servlet-api(4.0.0)

1.6 jstl
jstl(1.2)
standard(1.1.2)

1.7 jsp自定义标签依赖(必须与tomcat的版本一致)
tomcat-jsp-api

SSH整合

<plugins>
			<!--第一步就是配置maven-compiler-plugin插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>

注1:如何解决项目中不支持EL表达式的问题?将web.xml的DTD的版本由2.3升级到3.0即可。因为web2.3是不支持EL表达式的

2、配置文件:

spring-context.xml:
分模块
SSH整合

db.properties :

db.username=root
db.password=123
db.driverClass=com.mysql.jdbc.Driver
db.jdbcUrl=jdbc:mysql://127.0.0.1:3306/odinms?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
db.initialPoolSize=10
db.maxPoolSize=20
db.minPoolSize=5
db.maxIdleTime=60
db.acquireIncrement=5
db.maxStatements=0
db.idleConnectionTestPeriod=60
db.acquireRetryAttempts=30
db.breakAfterAcquireFailure=true
db.testConnectionOnCheckout=false

spring-hibernate.xml:

1、注册jdbc相关的配置文件
SSH整合

<context:property-placeholder location="classpath:db.properties" />

2、配置数据库连接池C3P0

SSH整合

	<!-- 2、配置数据库连接池C3P0 -->
	<!-- 注册数据库连接文件db.properties -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${db.username}"></property>
		<property name="password" value="${db.password}"></property>
		<property name="driverClass" value="${db.driverClass}"></property>
		<property name="jdbcUrl" value="${db.jdbcUrl}"></property>

		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${db.initialPoolSize}"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${db.maxPoolSize}"></property>
		<!--连接池中保留的最小连接数。 -->
		<property name="minPoolSize" value="${db.minPoolSize}" />
		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${db.maxIdleTime}" />

		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${db.acquireIncrement}" />

		<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。 
			所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 
			0 -->
		<property name="maxStatements" value="${db.maxStatements}" />

		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="${db.idleConnectionTestPeriod}" />

		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}" />

		<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。 
			如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
		<property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}" />

		<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod 
			或automaticTestTable 等方法来提升连接测试的性能。Default: false -->
		<property name="testConnectionOnCheckout" value="${db.breakAfterAcquireFailure}" />
	</bean>

3、配置sessionfactory相关信息

SSH整合

	<!-- 3、配置sessionfactory相关信息 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<!-- hibernate相关属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!--spring与Hibernate集成无法显示sql语句问题,请见集成后hibernate无法显示sql语句.txt -->
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<!-- 实体映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/zking/book/entity/Book.hbm.xml</value>
			</list>
		</property>
	</bean>

4、配置事务
SSH整合

	<!--1) 开启自动代理 -->
	<aop:aspectj-autoproxy /><!-- 动态代理 -->

	<!--2) 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!--3) 定义事务特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />

			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />

			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />

			<tx:method name="load*" propagation="REQUIRED" read-only="true" />
			<tx:method name="list*" propagation="REQUIRED" read-only="true" />
			<tx:method name="select*" propagation="REQUIRED" read-only="true" />
			<tx:method name="query*" propagation="REQUIRED" read-only="true" />

			<tx:method name="do*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<!--4) 定义切入点 -->
	<aop:config>
		<!-- pointcut属性用来定义一个切入点,分成四个部分理解 [* ][*..][*Biz][.*(..)] -->
		<!-- A: 返回类型,*表示返回类型不限 -->
		<!-- B: 包名,*..表示包名不限 -->
		<!-- C: 类或接口名,*Biz表示类或接口必须以Biz结尾 -->
		<!-- D: 方法名和参数,*(..)表示方法名不限,参数类型和个数不限 -->
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Biz.*(..))" />
	</aop:config>
	<!-- 声明式事务配置结束 -->

5、配置HibernateTemplate
SSH整合

	<!-- 5、配置HibernateTemplate -->
	<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
		id="hibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

6、分模块开发(base模块)
SSH整合

SSH整合

	<!-- 6、分模块开发(base模块) -->
	<!-- 抽象类: abstract="true" -->
	<bean class="com.zking.base.entity.BaseEntity" abstract="true" id="baseEntity"></bean>
	<bean class="com.zking.base.dao.BaseDao" abstract="true" id="baseDao">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<bean class="com.zking.base.biz.BaseBiz" abstract="true" id="baseBiz"></bean>
	<bean class="com.zking.base.web.BaseAction" abstract="true" id="baseAction"></bean>

资源文件:

SSH整合

3、web整合spring-hibernate、struts

配置请注意顺序:

1、整合spring
SSH整合


	<!-- 1、整合spring -->	
	<!-- Spring上下文applicationContext.xml加载 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-context.xml</param-value>
	</context-param>
	<!-- 启动Web容器时,自动装配ApplicationContext.xml的配置信息 -->
	<listener>
		<listener-class>
		org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

2、整合Struts2
SSH整合

	<!-- 2、整合Struts2 -->
	<!-- Struts2核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>

3、添加过滤器

SSH整合

	<!-- 3、添加过滤器 -->	
	<!-- 中文乱码过滤器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>