1 整合步骤
1.1 手动创建web.xml
首先新建一个maven工程,在创建的webapp路径下新建一个WEB-INF文件夹,并在该文件夹下新建一个web.xml文件
1.2 添加依赖
在pom.xml中添加关于Struts2、Spring、Hibernate等完成的依赖,添加依赖时注意它的四个原则(https://blog.youkuaiyun.com/W2612888/article/details/86726664)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itykd</groupId>
<artifactId>ssh-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 属性 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 依赖管理 -->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 导入 struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency>
<!-- servlet jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 设置编译版本为1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 可以灵活配置工程路径 -->
<path>/ssh</path>
<!-- 可以灵活配置端口号 -->
<port>8081</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.3 Hibernate文件的配置
1.3.1 Hibernate.cfg.xml
<?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.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--为了方便调试是否在运行hibernate时在日志中输出sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对日志中输出的sql语句进行格式化 -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">none</property>
<!-- 加载映射文件 -->
<mapping resource="com/itykd/entity/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.3.2 mapper.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.itykd.entity.Customer" table="cst_customer" optimistic-lock="version">
<id name="custId" type="java.lang.Long">
<column name="cust_id" />
<generator class="identity" />
</id>
<property name="custName" type="string">
<column name="cust_name" length="32" not-null="true"></column>
</property>
<property name="custUserId" type="java.lang.Long">
<column name="cust_user_id"></column>
</property>
<property name="custCreateId" type="java.lang.Long">
<column name="cust_create_id"></column>
</property>
<property name="custIndustry" type="string">
<column name="cust_industry" length="32"></column>
</property>
<property name="custLevel" type="string">
<column name="cust_level" length="32"></column>
</property>
<property name="custLinkman" type="string">
<column name="cust_linkman" length="64"></column>
</property>
<property name="custPhone" type="string">
<column name="cust_phone" length="64"></column>
</property>
<property name="custMobile" type="string">
<column name="cust_mobile" length="16"></column>
</property>
</class>
</hibernate-mapping>
1.4 applicationContext.xml
<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">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/maven" />
<property name="user" value="root" />
<property name="password" value="NULIFENDOU520" />
</bean>
<!-- 开启注解属性 -->
<context:annotation-config />
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 依赖dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- 创建工厂需要加载hibernate映射文件 -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="customerDao" class="com.itykd.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="customerService" class="com.itykd.service.impl.CustomerServiceImpl"/>
<bean id="customerAction" class="com.itykd.web.action.CustomerAction" scope="prototype" />
</beans>
1.5 struts.xml
<?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="demo" namespace="/" extends="struts-default" >
<action name="customer_*" class="customerAction" method="{1}">
<result name="success">/jsp/demo2.jsp</result>
</action>
</package>
</struts>
1.6 web.xml
在web.xml文件中添加struts2的核心过滤器(StrutsPrepareAndExecuteFilter)和spring的核心监听器(ContextLoaderListener)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>maven_start</display-name>
<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>
<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>
<!-- 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>
</web-app>
2 示例
2.1 web层
package com.itykd.web.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.itykd.entity.Customer;
import com.itykd.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport{
@Resource(name="customerService")
private CustomerService customerService;
private Customer customer;
private Long custId;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String findById(){
customer = customerService.getById(custId);
System.out.println("哈哈哈哈...");
return SUCCESS;
}
}
2.2 service层
package com.itykd.service.impl;
import javax.annotation.Resource;
import com.itykd.dao.CustomerDao;
import com.itykd.entity.Customer;
import com.itykd.service.CustomerService;
public class CustomerServiceImpl implements CustomerService{
@Resource(name="customerDao")
private CustomerDao customerDao;
@Override
public Customer getById(Long id) {
return customerDao.getById(id);
}
}
2.3 dao层
package com.itykd.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itykd.dao.CustomerDao;
import com.itykd.entity.Customer;
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{
@Override
public Customer getById(Long id) {
return this.getHibernateTemplate().get(Customer.class, id);
}
}