步骤
- 加入Spring
1.1. 添加依赖
<properties>
<!-- 统一源码的编码方式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一各个框架版本 -->
<struts.version>2.5.10</struts.version>
<spring.version>4.3.8.RELEASE</spring.version>
<hibernate.version>5.1.7.Final</hibernate.version>
</properties>
<dependencies>
<!-- Junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring 核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring整合ORM框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Struts2 核心依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- Struts2和Spring整合依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- Hibernate 核心依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<!-- C3P0 依赖 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5</version>
</dependency>
<!-- AspectJ依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- SLF4J依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<finalName>maven_ssh</finalName>
<plugins>
<!-- 打包时跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
1.2. 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssh</display-name>
<!-- 让spring随web启动而创建的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件位置参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 扩大session作用范围
注意:openSessionInView一定要在struts的filter之前调用,任何filter一定要在struts的filter之前调用
-->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 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>
<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>
1.3. 加入Spring的配置文件applicationContext.xml
<?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">
</beans>
- 加入Hibernate
2.1. 在类路径下加入hibernate.cfg.xml文件,在其中配置hibernate的基本属性
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD是约束,可以在核心包里面找 -->
<!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>
<!-- 输出底层sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 输出底层格式化语句 -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate帮创建表 需要配置之后
update:如果已经有表 更新 如果没有 创建 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置数据库方言
在mysql里面实现分页 关键字 limit,只能使用mysql
在oracle数据库 实现分页 rownum
让hibernate框架识别不同数据库的自己特有的语句 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
</session-factory>
</hibernate-configuration>
2.2. 同时建立持久化类和其对应的.hbm.xml文件,生成对应的数据表
public class Customer {
private String custId;
private String custName;
private String address;
public String getCustId() {
return custId;
}
public void setCustId(String custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入Hibernate映射文件约束 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- class指定POJO类和数据库表之间的对应 -->
<class name="it.playmaker.domain.Customer" table="t_customer" >
<!-- id指定数据库中表的主键,以及主键生成策略 -->
<id name="custId" type="java.lang.String" >
<column name="id" length="32"></column>
<generator class="uuid" />
</id>
<!-- property指定表字段和POJO中的属性的对应 -->
<property name="custName" column="custName" type="java.lang.String">
</property>
<property name="address" column="address" type="java.lang.String">
</property>
</class>
</hibernate-mapping>
2.3. Spring整合Hibernate,在applicationContext.xml中
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性值 -->
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate属性 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/hzy/ssh/dao/*.hbm.xml"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- xml管理事务 -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- 匹配业务类中方法名 -->
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 配置切点:具体哪些方法要增强(真正被增强的方法) -->
<aop:pointcut expression="execution(* com.hzy.ssh.service.*.*(..))" id="cut"/>
<!-- 配置切面:将增强逻辑作用到切点(通知+切入点)-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
</aop:config>
<!-- 1.开启注解驱动 2.在service类或者方法上使用注解@Transactional-->
<tx:annotation-driven transaction-manager="transactionManager" />
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh
jdbc.username=root
jdbc.password=root
- 加入Struts2
3.1. 加入Struts2的配置文件
<?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>
<constant name="struts.enable.Dynamicmethodinvocation" value="false"/>
<constant name="struts.devmode" value="true"/>
<package name="default" extends="struts-default" namespace="/">
</package>
</struts>
3.2. 整合Spring
applicationContext.xml
<!-- 配置action对象 -->
<bean id="customerAction" class="it.playmaker.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"></property>
</bean>
struts2.xml
<package name="default" extends="struts-default" namespace="/">
<action name="customerAction_*" class="customerAction" method="{1}">
<result name="list">/WEB-INF/views/list.jsp</result>
</action>
</package>
- 完成功能
在action类中实现RequestAware
接口
使用request.put(“返回的名字”,service.方法–[返回的值]);