@nextleaf
2018-06-30T02:10:31.000000Z
字数 16245
阅读 871
SSH
整合
Java
Spring
Hibernate
SSH的环境搭建,我的流程是:
Spring --> Hibernate --> Spring整合Hibernate --> Struts2 --> Spring整合Struts2
1.开发环境:
IntelliJ IDEA: 2018
Maven :3.0x
Java 8
2.pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.koali</groupId>
<artifactId>SSHDemo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SSHDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--测试相关的依赖-->
<!-- ********************** JUnit 4.12 ********************** -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<!--3.0的junit是使用编程的方式来进行测试,而junit4是使用注解的方式来运行junit-->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!--报错SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"而新添加的-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<!--<scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- ********************** Java Servlet API 4.0.1 ********************** -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
<!--对于scope=compile的情况(默认scope),也就是说这个项目在编译,测试,运行阶段都需要这个artifact对应的jar包在classpath中。
对于scope=provided的情况,则可以认为这个provided是目标容器已经provide这个artifact。
换句话说,它只影响到编译,测试阶段。
在编译测试阶段,我们需要这个artifact对应的jar包在classpath中,而在运行阶段,假定目标的容器(比如我们这里的liferay容器)已经提供了这个jar包,所以无需我们这个artifact对应的jar包了-->
</dependency>
<!-- ********************** Fastjson 1.2.47 ********************** -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- ********************** AspectJ Weaver 1.9.1 ********************** -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<!-- ********************** Apache Log4j 1.2.17 ********************** -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--报错:ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
添加用下面两个依赖-->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
<!--新添加的-->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<!-- ********************** Struts 2.5.16,之前是2.3.24.1 ********************** -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.16</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.16</version>
</dependency>
<!-- ********************** Java Transaction API 1.1 ********************** -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!-- ********************** MySQL 8.0.11,之前是5.1.38 ********************** -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- ********************** Druid 1.1.10 ********************** -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- ********************** Hibernate 5.3.1.Final,之前是5.0.6.Final ********************** -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.1.Final</version>
</dependency>
<!-- ********************** Spring 5.0.7.RELEASE之前是4.2.4.RELEASE ********************** -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<!-- <dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.5</version>
</dependency>-->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.15</version>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source><!-- 源代码使用的JDK开发版本 -->
<target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
<!-- 一般而言,target与source是保持一致的,
但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中需要没有使用低版本jdk中不支持的语法),
会存在target不同于source的情况 -->
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.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">
<!-- 配置Spring框架核心的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 该路径以webRoot作为根路径 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Struts2核心过滤器配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!--
报错Cannot resolve class 'StrutsPrepareAndExecuteFilter'
解决:https://blog.csdn.net/da_caoyuan/article/details/77744127
-->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置Spring监听器和Strut2过滤器
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"
xmlns:bean="http://www.alibaba.com/schema/stat"
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 http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
<!--扫描外部配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<!--2.Druid数据池-->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!--配置连接池属性-->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="3" />
<property name="minIdle" value="3" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>
<!--配置Hibernate整合Spring-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--注入连接池-->
<property name="dataSource" ref="ds"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!--配置Hibernate的映射文件-->
<property name="mappingResources">
<list>
<value>hbm/Product.hbm.xml</value>
<value>hbm/Department.hbm.xml</value>
<value>hbm/Employee.hbm.xml</value>
</list>
</property>
<property name="packagesToScan">
<list>
<value>com.koali.pojo</value>
</list>
</property>
</bean>
<!--Action交给Spring来管理-->
<bean id="productAction" class="com.koali.action.ProductAction" scope="prototype">
<property name="productService" ref="productService"/>
</bean>
<bean id="employeeAction" class="com.koali.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"/>
</bean>
<bean id="departmentAction" class="com.koali.action.DepartmentAction" scope="prototype">
<property name="departmentService" ref="departmentService"/>
</bean>
<!--配置Service层的类-->
<bean id="productService" class="com.koali.service.ProductService">
<property name="productDao" ref="productDao"/>
</bean>
<bean id="employeeService" class="com.koali.service.Impl.EmployeeServiceImpl">
<property name="employeeDao" ref="employeeDao"/>
</bean>
<bean id="departmentService" class="com.koali.service.Impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDao"/>
</bean>
<!--配置Dao层的类-->
<bean id="productDao" class="com.koali.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="employeeDao" class="com.koali.dao.Impl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="departmentDao" class="com.koali.dao.Impl.DepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置事务管理-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.编写Strut2的配置文件(strut.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="ssh" extends="struts-default" namespace="/">
<!-- <action name="product_*" class="productAction" method="{1}">
</action>-->
<action name="employee_*" class="employeeAction" method="{1}">
<result name="input">/index.jsp</result>
<result name="success" type="redirect">/frame.jsp</result>
<result name="findAll">/jsp/employee/list.jsp</result>
<result name="saveUI">/jsp/employee/add.jsp</result>
<result name="saveSuccess" type="redirectAction">employee_findAll.action</result>
<result name="editSuccess">/jsp/employee/edit.jsp</result>
<result name="updateSuccess" type="redirectAction">employee_findAll.action</result>
<result name="deleteSuccess" type="redirectAction">employee_findAll.action</result>
</action>
<action name="department_*" class="departmentAction" method="{1}">
<result name="findAll">/jsp/department/list.jsp</result>
<result name="saveUI">/jsp/department/add.jsp</result>
<result name="saveSuccess" type="redirectAction">department_findAll.action</result>
<result name="editSuccess">/jsp/department/edit.jsp</result>
<result name="updateSuccess" type="redirectAction">department_findAll.action</result>
<result name="deleteSuccess" type="redirectAction">department_findAll.action</result>
</action>
</package>
</struts>
5.最后编写简单的代码看看是否搭建成功
6.测试效果:
成功提交后,经过了action,service,dao,后台成功输出。
7.数据库
CREATE DATABASE `ssh`;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`did` int(11) NOT NULL AUTO_INCREMENT,
`dname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`ddesc` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`did`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`eid` int(11) NOT NULL AUTO_INCREMENT,
`ename` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`birthday` date NULL DEFAULT NULL,
`joinDate` date NULL DEFAULT NULL,
`eno` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`dno` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`eid`) USING BTREE,
INDEX `department`(`dno`) USING BTREE,
CONSTRAINT `FK_many-one-emp-dep` FOREIGN KEY (`dno`) REFERENCES `department` (`did`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`pname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`price` double NULL DEFAULT NULL,
PRIMARY KEY (`pid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, '电视机', 2300);
INSERT INTO `product` VALUES (2, '电冰箱', 3300);
-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES (1, '杂环化合物', '男', '2018-06-28', '2018-06-28', '20155785', 'huang', '123456', 1);
-- ----------------------------
-- Records of department
-- ----------------------------
INSERT INTO `department` VALUES (1, '部门1', '部门1的描述');
SET FOREIGN_KEY_CHECKS = 1;
https://blog.csdn.net/sysushui/article/details/73722870
https://blog.csdn.net/sysushui/article/details/68937005