1、所用SSH版本
struts2-2.3.24——>struts2-2.5.16
hibernate-4.3.10.Final——>hibernate-release-5.3.0.Final
spring-core-4.1.6.RELEASE——>spring-core-5.0.6
mysql-connector-java-5.1.33-bin——>mysql-connector-java-8.0.11
2、配置文件
web.xml:加载spring、struts2(实现整合)
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 1、项目名称 -->
<display-name>SSHTest1</display-name>
<!-- 2、乱码过滤器,处理乱码 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 3、加载spring框架 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 4、spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 5、加载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>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 6、错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 7、欢迎页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 8、session登出时间 -->
<session-config>
<session-timeout>300</session-timeout>
</session-config>
</web-app>
applicationContext.xml :主要实现spring和hibernate的整合(之前hibernate.cfg.xml和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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- IOC注解方式xml命名空间 -->
<context:annotation-config></context:annotation-config>
<!-- aop注解方式xml命名空间 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.service.impl"></context:component-scan>
<!-- 引入申明式事务支持的XML命名空间 -->
<!-- 读取外部属性文件,获取数据源参数 -->
<context:property-placeholder location="classpath:dataSource.properties"/>
<!-- 配置数据源datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<!-- 初始化连接数量; -->
<property name="initialSize" value="0" />
<!-- 最大并发连接数 -->
<property name="maxTotal" value="20" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="20" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="0" />
<!-- 最大等待时长 -->
<property name="maxWaitMillis" value="60000" />
<!-- 超过时间限制是否回收 -->
<property name="removeAbandonedOnBorrow" value="true" />
<!-- 超过时间限制多长; -->
<property name="removeAbandonedTimeout" value="180"/>
<!-- 数据源连接参数配置; -->
<property name="username" value="${db.username}"/>
<property name="url" value="${db.url}"/>
<property name="password" value="${db.password}"/>
<property name="driverClassName" value="${db.driverClassName}"/>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="net.xinqushi.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 初始化hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理 -->
<!-- 定义切面 -->
<aop:config>
<aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
struts.xml :配置struts2的action和results
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="all" namespace="/" extends="struts-default">
<global-results>
<result name="main">/main.jsp</result>
</global-results>
</package>
<include file="user.xml"></include>
<include file="album.xml"></include>
</struts>
user.xml :struts.xml的子文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="user" namespace="/user" extends="all">
<action name="*" class="net.xinqushi.action.UserAction" method="{1}">
<result name="login">/login.jsp</result>
<allowed-methods>add,login,logout,check</allowed-methods>
</action>
</package>
</struts>
album.xml :struts.xml的子文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="album" namespace="/album" extends="all">
<action name="*" class="net.xinqushi.action.AlbumAction" method="{1}">
<allowed-methods>add</allowed-methods>
</action>
</package>
</struts>
db.properties:数据库配置中占位符的信息
db.username=jack
db.url=jdbc:mysql://localhost:3306/album?serverTimezone=Asia/Shanghai&useSSL=true
db.password=12345678
db.driverClassName=com.mysql.cj.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true

被折叠的 条评论
为什么被折叠?



