[关闭]
@nextleaf 2018-06-29T18:10:31.000000Z 字数 16245 阅读 792

SSH整合Demo

SSH 整合 Java Spring Hibernate


SSH整合Demo排错日志

SSH的环境搭建,我的流程是:

  1. Spring --> Hibernate --> Spring整合Hibernate --> Struts2 --> Spring整合Struts2

1.开发环境
IntelliJ IDEA: 2018
Maven :3.0x
Java 8
2.pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.koali</groupId>
  6. <artifactId>SSHDemo</artifactId>
  7. <packaging>war</packaging>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>SSHDemo Maven Webapp</name>
  10. <url>http://maven.apache.org</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <!--测试相关的依赖-->
  16. <!-- ********************** JUnit 4.12 ********************** -->
  17. <!-- https://mvnrepository.com/artifact/junit/junit -->
  18. <dependency>
  19. <!--3.0的junit是使用编程的方式来进行测试,而junit4是使用注解的方式来运行junit-->
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.12</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
  26. <dependency>
  27. <groupId>org.slf4j</groupId>
  28. <artifactId>slf4j-api</artifactId>
  29. <version>1.7.25</version>
  30. </dependency>
  31. <!--报错SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"而新添加的-->
  32. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
  33. <dependency>
  34. <groupId>org.slf4j</groupId>
  35. <artifactId>slf4j-simple</artifactId>
  36. <version>1.7.25</version>
  37. <!--<scope>test</scope>-->
  38. </dependency>
  39. <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
  40. <dependency>
  41. <groupId>ch.qos.logback</groupId>
  42. <artifactId>logback-core</artifactId>
  43. <version>1.2.3</version>
  44. </dependency>
  45. <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
  46. <dependency>
  47. <groupId>ch.qos.logback</groupId>
  48. <artifactId>logback-classic</artifactId>
  49. <version>1.2.3</version>
  50. <scope>test</scope>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-test</artifactId>
  55. <version>5.0.7.RELEASE</version>
  56. </dependency>
  57. <!-- ********************** Java Servlet API 4.0.1 ********************** -->
  58. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  59. <dependency>
  60. <groupId>javax.servlet</groupId>
  61. <artifactId>javax.servlet-api</artifactId>
  62. <version>4.0.1</version>
  63. <scope>provided</scope>
  64. <!--对于scope=compile的情况(默认scope),也就是说这个项目在编译,测试,运行阶段都需要这个artifact对应的jar包在classpath中。
  65. 对于scope=provided的情况,则可以认为这个provided是目标容器已经provide这个artifact。
  66. 换句话说,它只影响到编译,测试阶段。
  67. 在编译测试阶段,我们需要这个artifact对应的jar包在classpath中,而在运行阶段,假定目标的容器(比如我们这里的liferay容器)已经提供了这个jar包,所以无需我们这个artifact对应的jar包了-->
  68. </dependency>
  69. <!-- ********************** Fastjson 1.2.47 ********************** -->
  70. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  71. <dependency>
  72. <groupId>com.alibaba</groupId>
  73. <artifactId>fastjson</artifactId>
  74. <version>1.2.47</version>
  75. </dependency>
  76. <!-- ********************** AspectJ Weaver 1.9.1 ********************** -->
  77. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
  78. <dependency>
  79. <groupId>org.aspectj</groupId>
  80. <artifactId>aspectjweaver</artifactId>
  81. <version>1.9.1</version>
  82. </dependency>
  83. <!-- ********************** Apache Log4j 1.2.17 ********************** -->
  84. <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  85. <dependency>
  86. <groupId>log4j</groupId>
  87. <artifactId>log4j</artifactId>
  88. <version>1.2.17</version>
  89. </dependency>
  90. <!--报错:ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
  91. 添加用下面两个依赖-->
  92. <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
  93. <dependency>
  94. <groupId>org.apache.logging.log4j</groupId>
  95. <artifactId>log4j-core</artifactId>
  96. <version>2.11.0</version>
  97. </dependency>
  98. <!--新添加的-->
  99. <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
  100. <dependency>
  101. <groupId>org.apache.logging.log4j</groupId>
  102. <artifactId>log4j-api</artifactId>
  103. <version>2.11.0</version>
  104. </dependency>
  105. <!-- ********************** Struts 2.5.16,之前是2.3.24.1 ********************** -->
  106. <dependency>
  107. <groupId>org.apache.struts</groupId>
  108. <artifactId>struts2-core</artifactId>
  109. <version>2.5.16</version>
  110. <exclusions>
  111. <exclusion>
  112. <groupId>javassist</groupId>
  113. <artifactId>javassist</artifactId>
  114. </exclusion>
  115. </exclusions>
  116. </dependency>
  117. <dependency>
  118. <groupId>org.apache.struts</groupId>
  119. <artifactId>struts2-spring-plugin</artifactId>
  120. <version>2.5.16</version>
  121. </dependency>
  122. <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
  123. <dependency>
  124. <groupId>org.apache.struts</groupId>
  125. <artifactId>struts2-convention-plugin</artifactId>
  126. <version>2.5.16</version>
  127. </dependency>
  128. <!-- ********************** Java Transaction API 1.1 ********************** -->
  129. <dependency>
  130. <groupId>javax.transaction</groupId>
  131. <artifactId>jta</artifactId>
  132. <version>1.1</version>
  133. </dependency>
  134. <!-- ********************** MySQL 8.0.11,之前是5.1.38 ********************** -->
  135. <dependency>
  136. <groupId>mysql</groupId>
  137. <artifactId>mysql-connector-java</artifactId>
  138. <version>8.0.11</version>
  139. </dependency>
  140. <!-- ********************** Druid 1.1.10 ********************** -->
  141. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  142. <dependency>
  143. <groupId>com.alibaba</groupId>
  144. <artifactId>druid</artifactId>
  145. <version>1.1.10</version>
  146. </dependency>
  147. <!-- ********************** Hibernate 5.3.1.Final,之前是5.0.6.Final ********************** -->
  148. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  149. <dependency>
  150. <groupId>org.hibernate</groupId>
  151. <artifactId>hibernate-core</artifactId>
  152. <version>5.3.1.Final</version>
  153. </dependency>
  154. <!-- ********************** Spring 5.0.7.RELEASE之前是4.2.4.RELEASE ********************** -->
  155. <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  156. <dependency>
  157. <groupId>org.springframework</groupId>
  158. <artifactId>spring-core</artifactId>
  159. <version>5.0.7.RELEASE</version>
  160. </dependency>
  161. <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
  162. <dependency>
  163. <groupId>org.springframework</groupId>
  164. <artifactId>spring-beans</artifactId>
  165. <version>5.0.7.RELEASE</version>
  166. </dependency>
  167. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  168. <dependency>
  169. <groupId>org.springframework</groupId>
  170. <artifactId>spring-context</artifactId>
  171. <version>5.0.7.RELEASE</version>
  172. </dependency>
  173. <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
  174. <dependency>
  175. <groupId>org.springframework</groupId>
  176. <artifactId>spring-web</artifactId>
  177. <version>5.0.7.RELEASE</version>
  178. </dependency>
  179. <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
  180. <dependency>
  181. <groupId>org.springframework</groupId>
  182. <artifactId>spring-orm</artifactId>
  183. <version>5.0.7.RELEASE</version>
  184. </dependency>
  185. <!-- https://mvnrepository.com/artifact/ognl/ognl -->
  186. <!-- <dependency>
  187. <groupId>ognl</groupId>
  188. <artifactId>ognl</artifactId>
  189. <version>3.2.5</version>
  190. </dependency>-->
  191. <dependency>
  192. <groupId>ognl</groupId>
  193. <artifactId>ognl</artifactId>
  194. <version>3.1.15</version>
  195. <!--<scope>test</scope>-->
  196. </dependency>
  197. </dependencies>
  198. <build>
  199. <plugins>
  200. <plugin>
  201. <artifactId>maven-compiler-plugin</artifactId>
  202. <version>3.7.0</version>
  203. <configuration>
  204. <source>1.8</source><!-- 源代码使用的JDK开发版本 -->
  205. <target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
  206. <!-- 一般而言,target与source是保持一致的,
  207. 但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中需要没有使用低版本jdk中不支持的语法),
  208. 会存在target不同于source的情况 -->
  209. </configuration>
  210. </plugin>
  211. <plugin>
  212. <artifactId>maven-war-plugin</artifactId>
  213. <version>3.2.2</version>
  214. <configuration>
  215. <failOnMissingWebXml>false</failOnMissingWebXml>
  216. </configuration>
  217. </plugin>
  218. </plugins>
  219. </build>
  220. </project>

3.web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6. id="WebApp_ID" version="2.5">
  7. <!-- 配置Spring框架核心的监听器 -->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <!-- 该路径以webRoot作为根路径 -->
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </context-param>
  16. <!-- Struts2核心过滤器配置 -->
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. <!--
  21. 报错Cannot resolve class 'StrutsPrepareAndExecuteFilter'
  22. 解决:https://blog.csdn.net/da_caoyuan/article/details/77744127
  23. -->
  24. </filter>
  25. <filter-mapping>
  26. <filter-name>struts2</filter-name>
  27. <url-pattern>/*</url-pattern>
  28. </filter-mapping>
  29. </web-app>

配置Spring监听器和Strut2过滤器
3.编写Spring配置文件(applicationContext.xml)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:bean="http://www.alibaba.com/schema/stat"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
  15. <!--扫描外部配置文件-->
  16. <context:property-placeholder location="classpath:jdbc.properties"/>
  17. <!--配置连接池-->
  18. <!--2.Druid数据池-->
  19. <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  20. <!--配置连接池属性-->
  21. <property name="driverClassName" value="${jdbc.driverClassName}"/>
  22. <property name="url" value="${jdbc.url}"/>
  23. <property name="username" value="${jdbc.username}"/>
  24. <property name="password" value="${jdbc.password}"/>
  25. <!-- 配置初始化大小、最小、最大 -->
  26. <property name="initialSize" value="3" />
  27. <property name="minIdle" value="3" />
  28. <property name="maxActive" value="20" />
  29. <!-- 配置获取连接等待超时的时间 -->
  30. <property name="maxWait" value="60000" />
  31. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  32. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  33. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  34. <property name="minEvictableIdleTimeMillis" value="300000" />
  35. <property name="validationQuery" value="SELECT 1" />
  36. <property name="testWhileIdle" value="true" />
  37. <property name="testOnBorrow" value="false" />
  38. <property name="testOnReturn" value="false" />
  39. <!-- 配置监控统计拦截的filters -->
  40. <property name="filters" value="stat" />
  41. </bean>
  42. <!--配置Hibernate整合Spring-->
  43. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  44. <!--注入连接池-->
  45. <property name="dataSource" ref="ds"/>
  46. <property name="hibernateProperties">
  47. <props>
  48. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  49. <prop key="hibernate.show_sql">true</prop>
  50. <prop key="hibernate.hbm2ddl.auto">update</prop>
  51. <prop key="hibernate.format_sql">true</prop>
  52. </props>
  53. </property>
  54. <!--配置Hibernate的映射文件-->
  55. <property name="mappingResources">
  56. <list>
  57. <value>hbm/Product.hbm.xml</value>
  58. <value>hbm/Department.hbm.xml</value>
  59. <value>hbm/Employee.hbm.xml</value>
  60. </list>
  61. </property>
  62. <property name="packagesToScan">
  63. <list>
  64. <value>com.koali.pojo</value>
  65. </list>
  66. </property>
  67. </bean>
  68. <!--Action交给Spring来管理-->
  69. <bean id="productAction" class="com.koali.action.ProductAction" scope="prototype">
  70. <property name="productService" ref="productService"/>
  71. </bean>
  72. <bean id="employeeAction" class="com.koali.action.EmployeeAction" scope="prototype">
  73. <property name="employeeService" ref="employeeService"/>
  74. </bean>
  75. <bean id="departmentAction" class="com.koali.action.DepartmentAction" scope="prototype">
  76. <property name="departmentService" ref="departmentService"/>
  77. </bean>
  78. <!--配置Service层的类-->
  79. <bean id="productService" class="com.koali.service.ProductService">
  80. <property name="productDao" ref="productDao"/>
  81. </bean>
  82. <bean id="employeeService" class="com.koali.service.Impl.EmployeeServiceImpl">
  83. <property name="employeeDao" ref="employeeDao"/>
  84. </bean>
  85. <bean id="departmentService" class="com.koali.service.Impl.DepartmentServiceImpl">
  86. <property name="departmentDao" ref="departmentDao"/>
  87. </bean>
  88. <!--配置Dao层的类-->
  89. <bean id="productDao" class="com.koali.dao.ProductDao">
  90. <property name="sessionFactory" ref="sessionFactory"/>
  91. </bean>
  92. <bean id="employeeDao" class="com.koali.dao.Impl.EmployeeDaoImpl">
  93. <property name="sessionFactory" ref="sessionFactory"/>
  94. </bean>
  95. <bean id="departmentDao" class="com.koali.dao.Impl.DepartmentDaoImpl">
  96. <property name="sessionFactory" ref="sessionFactory"/>
  97. </bean>
  98. <!--配置事务管理-->
  99. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  100. <property name="sessionFactory" ref="sessionFactory"/>
  101. </bean>
  102. <!--开启事务注解-->
  103. <tx:annotation-driven transaction-manager="transactionManager"/>
  104. </beans>

4.编写Strut2的配置文件(strut.xml)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
  3. <struts>
  4. <package name="ssh" extends="struts-default" namespace="/">
  5. <!-- <action name="product_*" class="productAction" method="{1}">
  6. </action>-->
  7. <action name="employee_*" class="employeeAction" method="{1}">
  8. <result name="input">/index.jsp</result>
  9. <result name="success" type="redirect">/frame.jsp</result>
  10. <result name="findAll">/jsp/employee/list.jsp</result>
  11. <result name="saveUI">/jsp/employee/add.jsp</result>
  12. <result name="saveSuccess" type="redirectAction">employee_findAll.action</result>
  13. <result name="editSuccess">/jsp/employee/edit.jsp</result>
  14. <result name="updateSuccess" type="redirectAction">employee_findAll.action</result>
  15. <result name="deleteSuccess" type="redirectAction">employee_findAll.action</result>
  16. </action>
  17. <action name="department_*" class="departmentAction" method="{1}">
  18. <result name="findAll">/jsp/department/list.jsp</result>
  19. <result name="saveUI">/jsp/department/add.jsp</result>
  20. <result name="saveSuccess" type="redirectAction">department_findAll.action</result>
  21. <result name="editSuccess">/jsp/department/edit.jsp</result>
  22. <result name="updateSuccess" type="redirectAction">department_findAll.action</result>
  23. <result name="deleteSuccess" type="redirectAction">department_findAll.action</result>
  24. </action>
  25. </package>
  26. </struts>

5.最后编写简单的代码看看是否搭建成功

简单的测试项目图.png
6.测试效果:

ssh2.gif
成功提交后,经过了action,service,dao,后台成功输出。

7.数据库

  1. CREATE DATABASE `ssh`;
  2. SET NAMES utf8mb4;
  3. SET FOREIGN_KEY_CHECKS = 0;
  4. -- ----------------------------
  5. -- Table structure for department
  6. -- ----------------------------
  7. DROP TABLE IF EXISTS `department`;
  8. CREATE TABLE `department` (
  9. `did` int(11) NOT NULL AUTO_INCREMENT,
  10. `dname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  11. `ddesc` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  12. PRIMARY KEY (`did`) USING BTREE
  13. ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
  14. -- ----------------------------
  15. -- Table structure for employee
  16. -- ----------------------------
  17. DROP TABLE IF EXISTS `employee`;
  18. CREATE TABLE `employee` (
  19. `eid` int(11) NOT NULL AUTO_INCREMENT,
  20. `ename` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  21. `sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  22. `birthday` date NULL DEFAULT NULL,
  23. `joinDate` date NULL DEFAULT NULL,
  24. `eno` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  25. `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  26. `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  27. `dno` int(11) NULL DEFAULT NULL,
  28. PRIMARY KEY (`eid`) USING BTREE,
  29. INDEX `department`(`dno`) USING BTREE,
  30. CONSTRAINT `FK_many-one-emp-dep` FOREIGN KEY (`dno`) REFERENCES `department` (`did`) ON DELETE RESTRICT ON UPDATE RESTRICT
  31. ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
  32. -- ----------------------------
  33. -- Table structure for product
  34. -- ----------------------------
  35. DROP TABLE IF EXISTS `product`;
  36. CREATE TABLE `product` (
  37. `pid` int(11) NOT NULL AUTO_INCREMENT,
  38. `pname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  39. `price` double NULL DEFAULT NULL,
  40. PRIMARY KEY (`pid`) USING BTREE
  41. ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
  42. -- ----------------------------
  43. -- Records of product
  44. -- ----------------------------
  45. INSERT INTO `product` VALUES (1, '电视机', 2300);
  46. INSERT INTO `product` VALUES (2, '电冰箱', 3300);
  47. -- ----------------------------
  48. -- Records of employee
  49. -- ----------------------------
  50. INSERT INTO `employee` VALUES (1, '杂环化合物', '男', '2018-06-28', '2018-06-28', '20155785', 'huang', '123456', 1);
  51. -- ----------------------------
  52. -- Records of department
  53. -- ----------------------------
  54. INSERT INTO `department` VALUES (1, '部门1', '部门1的描述');
  55. SET FOREIGN_KEY_CHECKS = 1;

另外两个教程

https://blog.csdn.net/sysushui/article/details/73722870
https://blog.csdn.net/sysushui/article/details/68937005

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注