想学习如何实现Activiti Modeler与自己开发的业务系统的整合,因此先把业务系统的框架搭起来。综合考虑目前Web应用开发框架,选择Spring + Spring MVC+Hibernate框架。本文就是搭建这样一个框架的基本流程,期间反复出现了各种莫名的错误,留档以备后查。
0、主要参考资料
(1)《框架篇:Spring+SpringMVC+hibernate整合开发》
(2)《spring+springmvc+hibernate整合实例》
(3)《SSH整合(Spring+SpringMVC+Hibernate)注解版》
(4)《spring+springmvc+hibernate 整合》
(5)《理解Spring+SpringMVC+Hibernate开发流程,附一定源码(一)》
1、环境准备
(1)操作系统:Windows7 x64 SP1
(2)Java开发环境JDK:jdk-10.0.2_windows-x64
(3)数据库系统MySQL:mysql-8.0.12-winx64
(4)构建工具Maven: apache-maven-3.5.4
(5)Web服务器Tomcat:Tomcat 9.0.10-64-bit-Windows
(6)集成开发环境Eclipse:Eclipse IDE for Java EE Developers-photon-R-win32-x86_64
2、创建项目
2.1 新建Maven web项目
在Eclipse中新建Maven项目,Archetype的类型选择【maven-archetype-webapp】,包名为【com.seu.liuds】,项目名称命名为【myssh】。设置JRE版本、Tomcat等信息,清除项目中出现的各类Error和Warning,修改index.jsp内容如下:
<html>
<body>
<h2>Hello, this is the welcome page!</h2>
</body>
</html>
加入到Tomcat中运行后结果如下。
2.2 构建MVC项目结构
按照MVC架构在src/main/java下创建controller、entity、reponsitory和service包,用于存放各类Java代码;在src/main/resources目录下创建config目录,用于存放项目配置文件;在src/main/webapp目录下创建views目录,用于存放前端页面,创建resources目录,用于存放页面资源文件。如下图所示。
3、导入jar包
编辑pom.xml文件,导入项目所需jar包。Eclipse会根据pom.xml文件内容自动到maven的中央仓库去下载相应的包。由于不熟悉框架需要导入什么jar包,在实施过程中先导入核心包,然后根据调试时提示的【NotFoundClass】错误信息,添加相关的依赖。
本文中参考《框架篇:Spring+SpringMVC+hibernate整合开发》一文,最终形成的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.seu.liuds</groupId>
<artifactId>myssh</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myssh Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 版本信息 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>10.0.0.2</java.version>
<springVersion>5.0.8.RELEASE</springVersion>
<hibernateVersion>5.3.4.Final</hibernateVersion>
<jstlVersion>1.2</jstlVersion>
<taglibVersion>1.1.2</taglibVersion>
<servletVersion>4.0.1</servletVersion>
<jsonVersion>1.9.13</jsonVersion>
<jacksonVersion>2.9.6</jacksonVersion>
<mysqlVersion>8.0.11</mysqlVersion>
<c3p0Version>0.9.1.2</c3p0Version>
<log4jVersion>1.2.17</log4jVersion>
<fileuploadVersion>1.3.3</fileuploadVersion>
<lombokVersion>1.18.2</lombokVersion>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springVersion}</version>
</dependency>
<!-- spring web + spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
<!-- Hibernate ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernateVersion}</version>
</dependency>
<!-- jsp页面使用的jstl支持 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstlVersion}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${taglibVersion}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servletVersion}</version>
<scope>provided</scope>
</dependency>
<!-- MySQL connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlVersion}</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0Version}</version>
</dependency>
<!-- json数据支持 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jsonVersion}</version>
</dependency>
<!-- 支持Java对象和json对象、xml之间的相互转换 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jacksonVersion}</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4jVersion}</version>
</dependency>
<!--文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${fileuploadVersion}</version>
</dependency>
<!-- lombok插件,支持@Data注解 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombokVersion}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>myssh</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source> <!-- 源代码开发版本 -->
<target>${java.version}</target> <!-- 生成目标class文件编译版本 -->
<compilerVersion>10</compilerVersion><!-- 使用的编译器的版本 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
4、配置SpringMVC
4.1 配置spring-mvc.xml
在myssh项目的src/main/resources/config文件夹下新建spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 第一步:开启注解 -->
<mvc:annotation-driven />
<!-- 第二步:扫描注解类,@controller注解标注的bean自动扫描 -->
<context:component-scan
base-package="com.seu.liuds.myssh.controller" />
<!-- 第三步:配置静态资源 -->
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/img/**" location="/resources/img/" />
<!-- 第四步:视图解析,定义页面文件前缀和后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 前缀 -->
<property name="prefix" value="views/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
需要注意以下三点:
(1)扫描包时要排除service层的类,不然在整合hibernate后,项目运行时会报错;
(2)package为【com.seu.liuds】,项目名【myssh】,扫描的地址为:【com.seu.liuds.myssh.controller】。
(3)xml文件中的.xsd文件的版本号要删除,否则容易出现错误。如原文件中的以下内容
……
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
……
应替换为
……
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
……
具体参考《为什么在Spring的配置里,最好不要配置xsd文件的版本号》一文。
4.2 配置web.xml
修改web.xml文件,载入SpringMVC的配置。
<?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_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- 默认网页:http://localhost:8080/ -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置SpringMVC:开始 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<!--配置容器在启动时加载这个servlet并实例化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!--拦截所有请求,拦截后web容器会将拦截的请求交给DispatcherServlet处理 -->
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置SpringMVC:结束 -->
</web-app>
4.3 测试SpringMVC配置
SpringMVC配置完成后,编写代码测试配置是否成功。在类包【com.seu.liuds.myssh.controller】中创建类文件【MainController.java】,代码如下。
package com.seu.liuds.myssh.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MainController {
@RequestMapping(value = "testmvc", method = RequestMethod.GET)
public String test() {
// 实际返回的是views/testmvc.jsp ,spring-mvc.xml中配置过前后缀
return "testmvc";
}
}
在src/main/webapp/views文件夹中创建前端页面文件【testmvc.jsp】,代码如下。
<html>
<body>
<h2>Hello, this is a test page for Spring MVC!</h2>
</body>
</html>
在Tomcat中运行项目,访问默认页面http://localhost:8080/myssh/和测试页面http://localhost:8080/myssh/testmvc,后者访问结果如下,说明SpringMVC配置成功。
5、整合Spring
5.1 配置Spring
在src/main/resources/config目录下新建applicationContext.xml文件,向其中加入以下内容。有关SpringMVC和Spring集成过程中可能出现的冲突,参考《Spring和SpringMVC扫描注解类的冲突问题》一文。
<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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描时跳过@Controller注解 -->
<context:component-scan base-package="com.seu.liuds.myssh">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
注意修改扫描的基础包名称为自己的项目设定。
5.2 修改web.xml
完善web.xml配置,主要包括:引入Spring配置文件、 Spring的监听器和字符集过滤。更新后的文件代码如下。
<?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_3_0.xsd"
version="3.0">
<!-- 地址为http://localhost:8080/ 默认网页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring MVC config start -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指向SpringMVC的配置文件 -->
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<!--配置容器在启动时加载这个servlet并实例化 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!--拦截所有请求,拦截后web容器会将拦截的请求交给DispatcherServlet处理 -->
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- spring MVC config end -->
<!-- Spring config start -->
<!--加载Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- 指向Spring的配置文件 -->
classpath:config/applicationContext.xml
</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 字符集过滤 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring config end -->
</web-app>
5.3 测试SpringMVC+Spring
在包【com.seu.liuds.myssh.service】中创建接口【ITestService.java】,其中包含一个test()方法,代码如下。
package com.seu.liuds.myssh.service;
public interface ITestService {
String test();
}
在service目录下新建【impl】文件夹,并在其下创建类TestServiceImpl实现TestService接口, 代码如下。返回的“testspring”字符实为testspring.jsp。
package com.seu.liuds.myssh.service.impl;
import org.springframework.stereotype.Service;
import com.seu.liuds.myssh.service.ITestService;
@Service
public class TestServiceImpl implements ITestService {
public String test() {
return "testspring";
}
}
在src/main/webapp/views文件夹中创建前端页面文件【testspring.jsp】,代码如下。
<html>
<body>
<h2>Hello, this is a test page for SpringMVC+Spring!</h2>
</body>
</html>
在Tomcat中运行项目,访问测试页面http://localhost:8080/myssh/testspring,结果如下,说明配置成功。
下面就要进行本次实验中出现问题最多的部分,整合Hibernate。
6、整合Hibernate
6.1 配置Hibernate
在src/main/resources/config目录下新建配置文件config.properties,存放关于数据库连接和hibernate的配置。代码如下。
#database connection config
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/sshdemo?useSSL=false&serverTimezone=GMT%2B8
jdbc.username = root
jdbc.password =
jdbc.databaseSchemaUpdate = true
#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
在src/main/resources/config目录下新建spring-hibernate.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"
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">
<context:property-placeholder location="classpath:config/config.properties" />
<!-- 配置数据源 c3p0 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 数据库连接参数设定 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="databaseSchemaUpdate"
value="${jdbc.databaseSchemaUpdate}" />
<!-- 请求超时时间 -->
<property name="checkoutTimeout" value="30000" />
<!-- 每30秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->
<property name="idleConnectionTestPeriod" value="30" />
<!-- 连接数据库连接池最大空闲时间30秒 -->
<property name="maxIdleTime" value="30" />
<!-- 连接池初始化连接数 -->
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->
<property name="acquireIncrement" value="5" />
</bean>
<!-- 配置hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源,相关信息看源码 -->
<property name="dataSource" ref="dataSource" />
<!-- hibernate配置信息 -->
<property name="hibernateProperties">
<props>
<!--指定数据库方言 -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!--在控制台显示执行的数据库操作语句 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!--在控制台显示执行的数据库操作语句格式 -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!--hibernate根据实体自动生成数据库表 -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 开启二级缓存 ehcache:暂时略过 -->
</props>
</property>
<!-- 扫描hibernate注解配置的entity -->
<property name="packagesToScan"
value="com.seu.liuds.demo.SSHDemo.entity" />
</bean>
<!-- 事物管理器配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
6.2 修改web.xml
加入
框架测试
dao = responsitory
model = entity
service
controller