共计 28274 个字符,预计需要花费 71 分钟才能阅读完成。
做了三年多的 JavaEE 开发了,在平时的 JavaEE 开发中,为了能够用最快的速度开发项目,一般都会选择使用 Struts2,SpringMVC,Spring,Hibernate,MyBatis 这些开源框架来开发项目,而这些框架一般不是单独使用的,经常是 Struts2+Spring3+Hibernate、SpringMVC+Spring+Hibernate、SpringMVC+Spring+Mybatis 这几种组合中的一种,也就是多个框架配合起来使用。今天来总结一下如何使用 Maven 搭建 Struts2+Spring3+Hibernate4 的整合开发环境。
Maven 权威指南_中文完整版清晰 PDF http://www.linuxidc.com/Linux/2014-06/103690.htm
Maven 3.1.0 发布,项目构建工具 http://www.linuxidc.com/Linux/2013-07/87403.htm
Linux 安装 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm
Maven3.0 配置和简单使用 http://www.linuxidc.com/Linux/2013-04/82939.htm
Ubuntu 下搭建 sun-jdk 和 Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm
Maven 使用入门 http://www.linuxidc.com/Linux/2012-11/74354.htm
一、建立 Maven 工程
第一步:
第二步:
第三步:
创建好的项目如下图所示:
第四步:
注意:这里的 JDK 要选择默认的,这样别人在使用的时候,如何 JDk 不一致的话也不会出错,如下图所示:
第五步:
创建 Maven 标准目录
src/main/java
src/main/resources
src/test/java
src/test/resources
第六步:
发布项目:Maven install
清除编译过的项目:Maven clean
Maven install 命令执行结果如下:
OK,Maven 工程创建成功!
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2015-02/114265p2.htm
二、搭建 Spring3 开发环境
2.1、下载 Spring3 需要的 jar 包
1.spring-core
2.spring-context
3.spring-jdbc
4.spring-beans
5.spring-web
6.spring-expression
7.spring-orm
在 pom.xml 中编写 Spring3 需要的包,maven 会自动下载这些包以及相关的依赖 jar 包。
1 <!-- spring3 --> | |
2 <dependency> | |
3 <groupId>org.springframework</groupId> | |
4 <artifactId>spring-core</artifactId> | |
5 <version>3.1.2.RELEASE</version> | |
6 </dependency> | |
7 <dependency> | |
8 <groupId>org.springframework</groupId> | |
9 <artifactId>spring-context</artifactId> | |
10 <version>3.1.2.RELEASE</version> | |
11 </dependency> | |
12 <dependency> | |
13 <groupId>org.springframework</groupId> | |
14 <artifactId>spring-jdbc</artifactId> | |
15 <version>3.1.2.RELEASE</version> | |
16 </dependency> | |
17 <dependency> | |
18 <groupId>org.springframework</groupId> | |
19 <artifactId>spring-beans</artifactId> | |
20 <version>3.1.2.RELEASE</version> | |
21 </dependency> | |
22 <dependency> | |
23 <groupId>org.springframework</groupId> | |
24 <artifactId>spring-web</artifactId> | |
25 <version>3.1.2.RELEASE</version> | |
26 </dependency> | |
27 <dependency> | |
28 <groupId>org.springframework</groupId> | |
29 <artifactId>spring-expression</artifactId> | |
30 <version>3.1.2.RELEASE</version> | |
31 </dependency> | |
32 <dependency> | |
33 <groupId>org.springframework</groupId> | |
34 <artifactId>spring-orm</artifactId> | |
35 <version>3.1.2.RELEASE</version> | |
36 </dependency> |
2.2、编写 Spring 配置文件
在 src/main/resources 目录下创建一个 spring.xml 文件,如下图所示:
spring.xml 文件的内容如下:
1 | |
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 xsi:schemaLocation="http://www.springframework.org/schema/beans | |
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
7 http://www.springframework.org/schema/context | |
8 http://www.springframework.org/schema/context/spring-context-3.0.xsd | |
9 "> | |
10 | |
11 <!-- 引入属性文件,config.properties 位于 src/main/resources 目录下 --> | |
12 <context:property-placeholder location="classpath:config.properties" /> | |
13 | |
14 <!-- 自动扫描 dao 和 service 包(自动注入) --> | |
15 <context:component-scan base-package="me.gacl.dao,me.gacl.service" /> | |
16 | |
17 </beans> |
在 src/main/resources 目录下创建一个 config.properties 文件,如下图所示:
config.properties 文件主要是用来编写一些系统的配置信息,例如数据库连接信息,config.properties 文件中的内容暂时先不编写,等整合到 Hibernate 时再编写具体的数据库连接信息。
2.3、编写单元测试
首先,在 src/main/Java 中创建 me.gacl.service 包,在包中编写一个 UserServiceI 接口,如下图所示:
代码如下:
1 package me.gacl.service; | |
2 | |
3 /** | |
4 * 测试 | |
5 * @author gacl | |
6 * | |
7 */ | |
8 public interface UserServiceI { 9 | |
10 /** | |
11 * 测试方法 | |
12 */ | |
13 void test(); | |
14 } |
然后,在 src/main/java 中创建 me.gacl.service.impl 包,在包中编写 UserServiceImpl 实现类,如下图所示:
代码如下:
1 package me.gacl.service.impl; | |
2 | |
3 import org.springframework.stereotype.Service; | |
4 | |
5 import me.gacl.service.UserServiceI; | |
6 //使用 Spring 提供的 @Service 注解将 UserServiceImpl 标注为一个 Service | |
7 | |
8 public class UserServiceImpl implements UserServiceI { 9 | |
10 | |
11 public void test() {12 System.out.println("Hello World!"); | |
13 } | |
14 | |
15 } |
进行单元测试时需要使用到 Junit,所以需要在 pom.xml 文件中添加 Junit 的 jar 包描述,如下:
1 <!-- Junit --> | |
2 <dependency> | |
3 <groupId>junit</groupId> | |
4 <artifactId>junit</artifactId> | |
5 <version>4.12</version> | |
6 <scope>test</scope> | |
7 </dependency> |
<scope>test</scope>这里的 test 表示测试时编译 src/main/test 文件夹中的文件,等发布的时候不编译。最后,在 src/main/test 中创建 me.gacl.test 包,在包中编写 TestSpring 类,如下图所示:
代码如下:
1 package me.gacl.test; | |
2 | |
3 import me.gacl.service.UserServiceI; | |
4 | |
5 import org.junit.Test; | |
6 import org.springframework.context.ApplicationContext; | |
7 import org.springframework.context.support.ClassPathXmlApplicationContext; | |
8 | |
9 public class TestSpring {10 | |
11 | |
12 public void test(){13 //通过 spring.xml 配置文件创建 Spring 的应用程序上下文环境 | |
14 ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml"); | |
15 //从 Spring 的 IOC 容器中获取 bean 对象 | |
16 UserServiceI userService = (UserServiceI) ac.getBean("userService"); | |
17 //执行测试方法 | |
18 userService.test(); | |
19 } | |
20 } |
JUnit Test 运行,结果如图所示:
2.4、在 web.xml 中配置 Spring 监听器
1 <!-- Spring 监听器 --> | |
2 <listener> | |
3 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
4 </listener> | |
5 <!-- Spring 配置文件位置 --> | |
6 <context-param> | |
7 <param-name>contextConfigLocation</param-name> | |
8 <param-value>classpath:spring.xml</param-value> | |
9 </context-param> |
在 tomcat 服务器中进行测试,先执行【Maven install】命令发布项目, 然后将 SSHE 项目部署到 tomcat 服务器,最后启动 tomcat 服务器
tomcat 服务器启动的过程中没有出现报错,输入地址:http://localhost:8080/SSHE/ 能够正常进行访问,就说明 Spring3 的开发环境搭建成功,如下图所示:
测试通过,Spring3 开发环境搭建成功!
做了三年多的 JavaEE 开发了,在平时的 JavaEE 开发中,为了能够用最快的速度开发项目,一般都会选择使用 Struts2,SpringMVC,Spring,Hibernate,MyBatis 这些开源框架来开发项目,而这些框架一般不是单独使用的,经常是 Struts2+Spring3+Hibernate、SpringMVC+Spring+Hibernate、SpringMVC+Spring+Mybatis 这几种组合中的一种,也就是多个框架配合起来使用。今天来总结一下如何使用 Maven 搭建 Struts2+Spring3+Hibernate4 的整合开发环境。
Maven 权威指南_中文完整版清晰 PDF http://www.linuxidc.com/Linux/2014-06/103690.htm
Maven 3.1.0 发布,项目构建工具 http://www.linuxidc.com/Linux/2013-07/87403.htm
Linux 安装 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm
Maven3.0 配置和简单使用 http://www.linuxidc.com/Linux/2013-04/82939.htm
Ubuntu 下搭建 sun-jdk 和 Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm
Maven 使用入门 http://www.linuxidc.com/Linux/2012-11/74354.htm
一、建立 Maven 工程
第一步:
第二步:
第三步:
创建好的项目如下图所示:
第四步:
注意:这里的 JDK 要选择默认的,这样别人在使用的时候,如何 JDk 不一致的话也不会出错,如下图所示:
第五步:
创建 Maven 标准目录
src/main/java
src/main/resources
src/test/java
src/test/resources
第六步:
发布项目:Maven install
清除编译过的项目:Maven clean
Maven install 命令执行结果如下:
OK,Maven 工程创建成功!
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2015-02/114265p2.htm
三、搭建 Struts2 开发环境并整合 Spring3
3.1、下载 Struts2 需要的 jar 包
1.strtus2-core
2.struts2-spring-plugin(struts2 和 Spring 整合时需要使用到的插件)
3.struts2-convention-plugin(使用了这个插件之后,就可以采用注解的方式配置 Struts 的 Action,免去了在 struts.xml 中的繁琐配置项)
4.struts2-config-browser-plugin(config-browser-plugin 插件不是必须的,但是使用了这个插件之后,就可以很方便的浏览项目中的所有 action 及其与 jsp view 的映射)
在 pom.xml 文件中编写 Struts2 所需要的 jar 包,Maven 会自动下载这些包
1 <!-- Struts2 的核心包 --> | |
2 <dependency> | |
3 <groupId>org.apache.struts</groupId> | |
4 <artifactId>struts2-core</artifactId> | |
5 <version>2.3.16</version> | |
6 <!-- | |
7 这里的 exclusions 是排除包,因为 Struts2 中有 Javassist,Hibernate 中也有 javassist, | |
8 所以如果要整合 Hibernate,一定要排除掉 Struts2 中的 javassist,否则就冲突了。 9 <exclusions> | |
10 <exclusion> | |
11 <groupId>javassist</groupId> | |
12 <artifactId>javassist</artifactId> | |
13 </exclusion> | |
14 </exclusions> | |
15 --> | |
16 </dependency> | |
17 <!-- convention-plugin 插件,使用了这个插件之后,就可以采用注解的方式配置 Action --> | |
18 <dependency> | |
19 <groupId>org.apache.struts</groupId> | |
20 <artifactId>struts2-convention-plugin</artifactId> | |
21 <version>2.3.20</version> | |
22 </dependency> | |
23 <!--config-browser-plugin 插件,使用了这个插件之后,就可以很方便的浏览项目中的所有 action 及其与 jsp view 的映射 --> | |
24 <dependency> | |
25 <groupId>org.apache.struts</groupId> | |
26 <artifactId>struts2-config-browser-plugin</artifactId> | |
27 <version>2.3.20</version> | |
28 </dependency> | |
29 <!-- Struts2 和 Spring 整合插件 --> | |
30 <dependency> | |
31 <groupId>org.apache.struts</groupId> | |
32 <artifactId>struts2-spring-plugin</artifactId> | |
33 <version>2.3.4.1</version> | |
34 </dependency> |
3.2、编写 Struts.xml 配置文件
在 src/main/resources 目录下创建一个 struts.xml 文件,如下图所示:
struts.xml 文件中的内容如下:
1 | |
2 | |
3 <struts> | |
4 | |
5 <!-- 指定由 spring 负责 action 对象的创建 --> | |
6 <constant name="struts.objectFactory" value="spring" /> | |
7 <!-- 所有匹配 *.action 的请求都由 struts2 处理 --> | |
8 <constant name="struts.action.extension" value="action" /> | |
9 <!-- 是否启用开发模式(开发时设置为 true, 发布到生产环境后设置为 false) --> | |
10 <constant name="struts.devMode" value="true" /> | |
11 <!-- struts 配置文件改动后,是否重新加载(开发时设置为 true, 发布到生产环境后设置为 false) --> | |
12 <constant name="struts.configuration.xml.reload" value="true" /> | |
13 <!-- 设置浏览器是否缓存静态内容(开发时设置为 false, 发布到生产环境后设置为 true) --> | |
14 <constant name="struts.serve.static.browserCache" value="false" /> | |
15 <!-- 请求参数的编码方式 --> | |
16 <constant name="struts.i18n.encoding" value="utf-8" /> | |
17 <!-- 每次 HTTP 请求系统都重新加载资源文件,有助于开发(开发时设置为 true, 发布到生产环境后设置为 false) --> | |
18 <constant name="struts.i18n.reload" value="true" /> | |
19 <!-- 文件上传最大值 --> | |
20 <constant name="struts.multipart.maxSize" value="104857600" /> | |
21 <!-- 让 struts2 支持动态方法调用, 使用叹号访问方法 --> | |
22 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> | |
23 <!-- Action 名称中是否还是用斜线 --> | |
24 <constant name="struts.enable.SlashesInActionNames" value="false" /> | |
25 <!-- 允许标签中使用表达式语法 --> | |
26 <constant name="struts.tag.altSyntax" value="true" /> | |
27 <!-- 对于 WebLogic,Orion,OC4J 此属性应该设置成 true --> | |
28 <constant name="struts.dispatcher.parametersWorkaround" value="false" /> | |
29 | |
30 <package name="basePackage" extends="struts-default"> | |
31 | |
32 | |
33 </package> | |
34 | |
35 </struts> |
3.3、在 web.xml 中配置 Struts2 的过滤器
1 <!-- Struts2 的核心过滤器配置 --> | |
2 <filter> | |
3 <filter-name>struts2</filter-name> | |
4 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> | |
5 </filter> | |
6 <!-- Struts2 过滤器拦截所有的.action 请求 --> | |
7 <filter-mapping> | |
8 <filter-name>struts2</filter-name> | |
9 <url-pattern>*.action</url-pattern> | |
10 </filter-mapping> |
3.4、编写测试
首先,在 src/main/java 中创建 me.gacl.action 包,在包中编写一个 TestAction 类,如下图所示:
代码如下:
1 package me.gacl.action; | |
2 | |
3 import me.gacl.service.UserServiceI; | |
4 | |
5 import org.apache.struts2.convention.annotation.Action; | |
6 import org.apache.struts2.convention.annotation.Namespace; | |
7 import org.apache.struts2.convention.annotation.ParentPackage; | |
8 import org.springframework.beans.factory.annotation.Autowired; | |
9 | |
10 | |
11 //使用 convention-plugin 插件提供的 @Action 注解将一个普通 java 类标注为一个可以处理用户请求的 Action,Action 的名字为 struts2Test | |
12 //使用 convention-plugin 插件提供的 @Namespace 注解为这个 Action 指定一个命名空间 | |
13 public class TestAction {14 | |
15 /** | |
16 * 注入 userService | |
17 */ | |
18 | |
19 private UserServiceI userService; | |
20 | |
21 /** | |
22 * http://localhost:8080/SSHE/strust2Test!test.action | |
23 * MethodName: test | |
24 * Description: | |
25 * @author xudp | |
26 */ | |
27 public void test(){28 System.out.println("进入 TestAction"); | |
29 userService.test(); | |
30 } | |
31 } |
这里使用 @Autowired 注解将 userService 注入到 UserAction 中。
测试 Struts2 的开发环境是否搭建成功,先执行【Maven install】操作, 然后部署到 tomcat 服务器,最后启动 tomcat 服务器运行,
输入访问地址:http://localhost:8080/SSHE/strust2Test!test.action,访问结果如下:
测试通过,Struts2 的开发环境搭建并整合 Spring 成功! 这里提一下遇到的问题,我执行完 Maven install 命令之后,重新发布到 tomcat 服务器运行,第一次运行时出现了找不到 action 的 404 错误,后来就先执行 Maven clean,然后 clean 一下项目,再执行 Maven install 命令重新编译项目,然后再发布到 tomcat 服务器中运行,这次就可以正常访问到 action 了,使用 Maven 总是会遇到一些奇怪的问题,好在凭借着一些平时积累的解决问题的经验把问题解决了。
四、搭建 Hibernate4 开发环境并整合 Spring3
4.1、下载 Hibernate4 需要的 jar 包
1.hibernate-core
在 pom.xml 文件中编写 Hibernate4 所需要的 jar 包,Maven 会自动下载这些包。
1 <!-- hibernate4 --> | |
2 <dependency> | |
3 <groupId>org.hibernate</groupId> | |
4 <artifactId>hibernate-core</artifactId> | |
5 <version>4.1.7.Final</version> | |
6 </dependency> |
注意:一定要排除掉 Struts2 中的 Javassist,否则就冲突了。
4.2、添加数据库驱动 jar 包
我们知道,Hibernate 是用于和数据库交互的,应用系统所有的 CRUD 操作都要通过 Hibernate 来完成。既然要连接数据库,那么就要使用到相关的数据库驱动,所以需要加入数据库驱动的 jar 包,根据自身项目使用的数据库在 pom.xml 文件中编写相应的数据库驱动 jar:
MySQL 数据库驱动 jar:
1 <!-- mysql 驱动包 --> | |
2 <dependency> | |
3 <groupId>mysql</groupId> | |
4 <artifactId>mysql-connector-java</artifactId> | |
5 <version>5.1.34</version> | |
6 </dependency> |
SQLServer 数据库驱动 jar:
1 <!-- SQLServer 数据库驱动包 --> | |
2 <dependency> | |
3 <groupId>net.sourceforge.jtds</groupId> | |
4 <artifactId>jtds</artifactId> | |
5 <version>1.3.1</version> | |
6 </dependency> |
这里要说一下使用 Maven 管理 Oracle JDBC 驱动的问题了,正常情况下,Maven 在下载 oracle 数据库驱动时会出错,如下图所示:
这是 由于 Oracle 授权问题,Maven3 不提供 Oracle JDBC driver,为了在 Maven 项目中应用 Oracle JDBC driver, 必须手动添加到本地仓库。
解决办法:先从网上下载 Oracle 的驱动包,然后通过 Maven 命令放到本地库中去:
安装命令:
mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar
例如把位于 F:\oracle 驱动 \ojdbc6.jar 添加到本地仓库中
执行命令:
mvn install:install-file -Dfile=F:/oracle 驱动 /ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar
如下图所示:
然后在 pom.xml 文件中编写 ojdbc6.jar 包的 <dependency> 信息,如下所示:
1 <!--Oracle 数据库驱动包,针对 Oracle11.2 的 ojdbc6.jar --> | |
2 <dependency> | |
3 <groupId>com.oracle</groupId> | |
4 <artifactId>ojdbc6</artifactId> | |
5 <version>11.2.0.1.0</version> | |
6 </dependency> |
由于我们已经将 ojdbc6.jar 包加入到本地仓库中了,因此这次可以正常使用针对 Oracle 数据库的驱动包了。如下图所示:
4.3、添加数据库连接池 jar 包
在平时开发中,我们一般都会使用数据库连接池,应用系统初始化时,由数据库连接池向数据库申请一定数量的数据库连接,然后放到一个连接池中,当需要操作数据库时,就从数据库连接池中取出一个数据库连接,通过从连接池中获取到的数据库连接对象连接上数据库,然后进行 CRUD 操作,关于数据库连接池的选择,常用的有 DBCP,C3P0 和 Druid,这里我们使用 Druid 作为我们的数据库连接池。这三种连接池各自有各自的特点,自己熟悉哪个就用哪个,萝卜白菜,各有所爱。
在 pom.xml 文件中编写 Druid 的 jar 包,Maven 会自动下载,如下:
1 <!--Druid 连接池包 --> | |
2 <dependency> | |
3 <groupId>com.alibaba</groupId> | |
4 <artifactId>druid</artifactId> | |
5 <version>1.0.12</version> | |
6 </dependency> |
4.4、添加 aspectjweaver 包
使用 Spring 的 aop 时需要使用到 aspectjweaver 包,所以需要添加 aspectjweaver 包,在 pom.xml 文件中添加 aspectjweaver 的 jar 包,Maven 会自动下载,如下:
1 <!--aspectjweaver 包 --> | |
2 <dependency> | |
3 <groupId>org.aspectj</groupId> | |
4 <artifactId>aspectjweaver</artifactId> | |
5 <version>1.8.5</version> | |
6 </dependency> |
4.5、编写连接数据库的配置信息
之前我们在 src/main/resources 目录下创建了一个 config.properties 文件,里面的内容是空的,现在我们就在这个 config.properties 文件中编写连接数据库需要使用到的相关信息,如下所示:
1 #hibernate.dialect=org.hibernate.dialect.OracleDialect | |
2 #driverClassName=oracle.jdbc.driver.OracleDriver | |
3 #validationQuery=SELECT 1 FROM DUAL | |
4 #jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl | |
5 #jdbc_username=gacl | |
6 #jdbc_password=xdp | |
7 | |
8 hibernate.dialect=org.hibernate.dialect.MySQLDialect | |
9 driverClassName=com.mysql.jdbc.Driver | |
10 validationQuery=SELECT 1 | |
11 jdbc_url=jdbc:mysql://localhost:3306/sshe?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull | |
12 jdbc_username=root | |
13 jdbc_password=XDP | |
14 | |
15 #hibernate.dialect=org.hibernate.dialect.SQLServerDialect | |
16 #driverClassName=net.sourceforge.jtds.jdbc.Driver | |
17 #validationQuery=SELECT 1 | |
18 #jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sshe | |
19 #jdbc_username=sa | |
20 #jdbc_password=123456 | |
21 | |
22 #jndiName=java:comp/env/dataSourceName | |
23 | |
24 hibernate.hbm2ddl.auto=update | |
25 hibernate.show_sql=true | |
26 hibernate.format_sql=true |
4.6、编写 Hibernate 与 Spring 整合的配置文件
在 src/main/resources 目录下新建一个 spring-hibernate.xml 文件,如下图所示:
spring-hibernate.xml 文件的内容如下:
1 | |
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" 3 http://www.springframework.org/schema/beans | |
4 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
5 http://www.springframework.org/schema/tx | |
6 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd | |
7 http://www.springframework.org/schema/aop | |
8 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd | |
9 "> | |
10 | |
11 <!-- JNDI 方式配置数据源 --> | |
12 <!-- | |
13 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> | |
14 <property name="jndiName" value="${jndiName}"></property> | |
15 </bean> | |
16 --> | |
17 | |
18 <!-- 配置数据源 --> | |
19 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> | |
20 <property name="url" value="${jdbc_url}" /> | |
21 <property name="username" value="${jdbc_username}" /> | |
22 <property name="password" value="${jdbc_password}" /> | |
23 | |
24 <!-- 初始化连接大小 --> | |
25 <property name="initialSize" value="0" /> | |
26 <!-- 连接池最大使用连接数量 --> | |
27 <property name="maxActive" value="20" /> | |
28 <!-- 连接池最大空闲 --> | |
29 <property name="maxIdle" value="20" /> | |
30 <!-- 连接池最小空闲 --> | |
31 <property name="minIdle" value="0" /> | |
32 <!-- 获取连接最大等待时间 --> | |
33 <property name="maxWait" value="60000" /> | |
34 | |
35 <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> | |
36 | |
37 <property name="validationQuery" value="${validationQuery}" /> | |
38 <property name="testOnBorrow" value="false" /> | |
39 <property name="testOnReturn" value="false" /> | |
40 <property name="testWhileIdle" value="true" /> | |
41 | |
42 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> | |
43 <property name="timeBetweenEvictionRunsMillis" value="60000" /> | |
44 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> | |
45 <property name="minEvictableIdleTimeMillis" value="25200000" /> | |
46 | |
47 <!-- 打开 removeAbandoned 功能 --> | |
48 <property name="removeAbandoned" value="true" /> | |
49 <!-- 1800 秒,也就是 30 分钟 --> | |
50 <property name="removeAbandonedTimeout" value="1800" /> | |
51 <!-- 关闭 abanded 连接时输出错误日志 --> | |
52 <property name="logAbandoned" value="true" /> | |
53 | |
54 <!-- 监控数据库 --> | |
55 <!-- <property name="filters" value="stat" /> --> | |
56 <property name="filters" value="mergeStat" /> | |
57 </bean> | |
58 | |
59 <!-- 配置 hibernate session 工厂 --> | |
60 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> | |
61 <property name="dataSource" ref="dataSource" /> | |
62 <property name="hibernateProperties"> | |
63 <props> | |
64 <!-- web 项目启动时是否更新表结构 --> | |
65 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> | |
66 <!-- 系统使用的数据库方言,也就是使用的数据库类型 --> | |
67 <prop key="hibernate.dialect">${hibernate.dialect}</prop> | |
68 <!-- 是否打印 Hibernate 生成的 SQL 到控制台 --> | |
69 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> | |
70 <!-- 是否格式化打印出来的 SQL --> | |
71 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> | |
72 </props> | |
73 </property> | |
74 | |
75 <!-- 自动扫描注解方式配置的 hibernate 类文件 --> | |
76 <property name="packagesToScan"> | |
77 <list> | |
78 <value>me.gacl.model</value> | |
79 </list> | |
80 </property> | |
81 | |
82 <!-- 自动扫描 hbm 方式配置的 hibernate 文件和.hbm 文件 --> | |
83 <!-- | |
84 <property name="mappingDirectoryLocations"> | |
85 <list> | |
86 <value>classpath:me/gacl/model/hbm</value> | |
87 </list> | |
88 </property> | |
89 --> | |
90 </bean> | |
91 | |
92 <!-- 配置事务管理器 --> | |
93 <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> | |
94 <property name="sessionFactory" ref="sessionFactory"></property> | |
95 </bean> | |
96 | |
97 <!-- 注解方式配置事物 --> | |
98 <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> | |
99 | |
100 <!-- 拦截器方式配置事物 --> | |
101 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> | |
102 <tx:attributes> | |
103 <!-- 以如下关键字开头的方法使用事物 --> | |
104 <tx:method name="add*" /> | |
105 <tx:method name="save*" /> | |
106 <tx:method name="update*" /> | |
107 <tx:method name="modify*" /> | |
108 <tx:method name="edit*" /> | |
109 <tx:method name="delete*" /> | |
110 <tx:method name="remove*" /> | |
111 <tx:method name="repair" /> | |
112 <tx:method name="deleteAndRepair" /> | |
113 <!-- 以如下关键字开头的方法不使用事物 --> | |
114 <tx:method name="get*" propagation="SUPPORTS" /> | |
115 <tx:method name="find*" propagation="SUPPORTS" /> | |
116 <tx:method name="load*" propagation="SUPPORTS" /> | |
117 <tx:method name="search*" propagation="SUPPORTS" /> | |
118 <tx:method name="datagrid*" propagation="SUPPORTS" /> | |
119 <!-- 其他方法不使用事物 --> | |
120 <tx:method name="*" propagation="SUPPORTS" /> | |
121 </tx:attributes> | |
122 </tx:advice> | |
123 <!-- 切面,将事物用在哪些对象上 --> | |
124 <aop:config> | |
125 <aop:pointcut id="transactionPointcut" expression="execution(* me.gacl.service..*Impl.*(..))" /> | |
126 <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> | |
127 </aop:config> | |
128 | |
129 </beans> |
4.7、编写单元测试代码
1、在 MySQL 中创建 sshe 数据库
SQL 脚本:
CREATE DATABASE SSHE;
2、在 src/main/java 中创建 me.gac.model 包,在包中编写一个 User 类,如下图所示:
代码如下:
1 package me.gacl.model; | |
2 | |
3 import java.util.Date; | |
4 | |
5 import javax.persistence.Column; | |
6 import javax.persistence.Entity; | |
7 import javax.persistence.Id; | |
8 import javax.persistence.Table; | |
9 import javax.persistence.Temporal; | |
10 import javax.persistence.TemporalType; | |
11 | |
12 | |
13 (name = "T_USER", schema = "SSHE") | |
14 public class User implements java.io.Serializable {15 | |
16 // Fields | |
17 private String id; | |
18 private String name; | |
19 private String pwd; | |
20 private Date createdatetime; | |
21 private Date modifydatetime; | |
22 | |
23 // Constructors | |
24 | |
25 /** default constructor */ | |
26 public User() {27 } | |
28 | |
29 /** minimal constructor */ | |
30 public User(String id, String name, String pwd) {31 this.id = id; | |
32 this.name = name; | |
33 this.pwd = pwd; | |
34 } | |
35 | |
36 /** full constructor */ | |
37 public User(String id, String name, String pwd, Date createdatetime, Date modifydatetime) {38 this.id = id; | |
39 this.name = name; | |
40 this.pwd = pwd; | |
41 this.createdatetime = createdatetime; | |
42 this.modifydatetime = modifydatetime; | |
43 } | |
44 | |
45 // Property accessors | |
46 | |
47 (name = "ID", unique = true, nullable = false, length = 36) | |
48 public String getId() {49 return this.id; | |
50 } | |
51 | |
52 public void setId(String id) {53 this.id = id; | |
54 } | |
55 | |
56 (name = "NAME",nullable = false, length = 100) | |
57 public String getName() {58 return this.name; | |
59 } | |
60 | |
61 public void setName(String name) {62 this.name = name; | |
63 } | |
64 | |
65 (name = "PWD", nullable = false, length = 32) | |
66 public String getPwd() {67 return this.pwd; | |
68 } | |
69 | |
70 public void setPwd(String pwd) {71 this.pwd = pwd; | |
72 } | |
73 | |
74 (TemporalType.TIMESTAMP) | |
75 (name = "CREATEDATETIME", length = 7) | |
76 public Date getCreatedatetime() {77 return this.createdatetime; | |
78 } | |
79 | |
80 public void setCreatedatetime(Date createdatetime) {81 this.createdatetime = createdatetime; | |
82 } | |
83 | |
84 (TemporalType.TIMESTAMP) | |
85 (name = "MODIFYDATETIME", length = 7) | |
86 public Date getModifydatetime() {87 return this.modifydatetime; | |
88 } | |
89 | |
90 public void setModifydatetime(Date modifydatetime) {91 this.modifydatetime = modifydatetime; | |
92 } | |
93 } |
3、在 src/main/java 中创建 me.gacl.dao 包,在包中编写一个 UserDaoI 接口,如下图所示:
代码如下:
1 package me.gacl.dao; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import me.gacl.model.User; | |
6 | |
7 public interface UserDaoI { 8 | |
9 /** | |
10 * 保存用户 | |
11 * @param user | |
12 * @return | |
13 */ | |
14 Serializable save(User user); | |
15 } |
在 src/main/java 中创建 me.gacl.dao.impl 包,在包中编写 UserDaoImpl 实现类,如下图所示:
代码如下:
1 package me.gacl.dao.impl; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import org.hibernate.SessionFactory; | |
6 import org.springframework.beans.factory.annotation.Autowired; | |
7 import org.springframework.stereotype.Repository; | |
8 | |
9 import me.gacl.dao.UserDaoI; | |
10 import me.gacl.model.User; | |
11 | |
12 | |
13 public class UserDaoImpl implements UserDaoI {14 | |
15 /** | |
16 * 使用 @Autowired 注解将 sessionFactory 注入到 UserDaoImpl 中 | |
17 */ | |
18 | |
19 private SessionFactory sessionFactory; | |
20 | |
21 | |
22 public Serializable save(User user) {23 return sessionFactory.getCurrentSession().save(user); | |
24 } | |
25 } |
这里使用 @Repository(“userDao”)注解完成 dao 注入,使用 @Autowired 注解将 sessionFactory 注入到 UserDaoImpl 中。
4、在之前创建好的 UserServiceI 接口中添加一个 save 方法的定义,如下:
1 package me.gacl.service; | |
2 | |
3 import java.io.Serializable; | |
4 import me.gacl.model.User; | |
5 | |
6 /** | |
7 * 测试 | |
8 * @author gacl | |
9 * | |
10 */ | |
11 public interface UserServiceI {12 | |
13 /** | |
14 * 测试方法 | |
15 */ | |
16 void test(); | |
17 | |
18 /** | |
19 * 保存用户 | |
20 * @param user | |
21 * @return | |
22 */ | |
23 Serializable save(User user); | |
24 } |
5、在 UserServiceImpl 类中实现 save 方法,如下:
1 package me.gacl.service.impl; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import org.springframework.beans.factory.annotation.Autowired; | |
6 import org.springframework.stereotype.Service; | |
7 | |
8 import me.gacl.dao.UserDaoI; | |
9 import me.gacl.model.User; | |
10 import me.gacl.service.UserServiceI; | |
11 //使用 Spring 提供的 @Service 注解将 UserServiceImpl 标注为一个 Service | |
12 | |
13 public class UserServiceImpl implements UserServiceI {14 | |
15 /** | |
16 * 注入 userDao | |
17 */ | |
18 | |
19 private UserDaoI userDao; | |
20 | |
21 | |
22 public void test() {23 System.out.println("Hello World!"); | |
24 } | |
25 | |
26 | |
27 public Serializable save(User user) {28 return userDao.save(user); | |
29 } | |
30 } |
6、在 src/main/test 下的 me.gacl.test 包中编写 TestHibernate 类,代码如下:
1 package me.gacl.test; | |
2 | |
3 import java.util.Date; | |
4 import java.util.UUID; | |
5 | |
6 import me.gacl.model.User; | |
7 import me.gacl.service.UserServiceI; | |
8 | |
9 import org.junit.Before; | |
10 import org.junit.Test; | |
11 import org.springframework.context.ApplicationContext; | |
12 import org.springframework.context.support.ClassPathXmlApplicationContext; | |
13 | |
14 public class TestHibernate {15 | |
16 private UserServiceI userService; | |
17 | |
18 /** | |
19 * 这个 before 方法在所有的测试方法之前执行,并且只执行一次 | |
20 * 所有做 Junit 单元测试时一些初始化工作可以在这个方法里面进行 | |
21 * 比如在 before 方法里面初始化 ApplicationContext 和 userService | |
22 */ | |
23 | |
24 public void before(){25 ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"}); | |
26 userService = (UserServiceI) ac.getBean("userService"); | |
27 } | |
28 | |
29 | |
30 public void testSaveMethod(){31 //ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"}); | |
32 //UserServiceI userService = (UserServiceI) ac.getBean("userService"); | |
33 User user = new User(); | |
34 user.setId(UUID.randomUUID().toString().replaceAll("-", "")); | |
35 user.setName("孤傲苍狼"); | |
36 user.setPwd("123"); | |
37 user.setCreatedatetime(new Date()); | |
38 userService.save(user); | |
39 } | |
40 } |
执行 Junit 单元测试,如下所示:
测试通过,再看看 sshe 数据库,如下图所示:
Hibernate 在执行过程中,先帮我们在 sshe 数据库中创建一张 t_user 表,t_user 的表结构根据 User 实体类中的属性定义来创建的,然后再将数据插入到 t_user 表中,如下图所示:
到此,Hibernate4 开发环境的搭建并且与 Spring 整合的工作算是全部完成并且测试通过了。
五、三大框架综合测试
经过前面的四大步骤,我们已经成功地搭建好基于 struts2+hibernate4+spring3 这三大框架的整合开发环境,下面我们来综合测试一下三大框架配合使用进行开发的效果。
5.1、完善 web.xml 文件中的配置
1 | |
2 <web-app version="3.0" xmlns="http://Java.sun.com/xml/ns/javaee" | |
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | |
5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | |
6 <display-name></display-name> | |
7 <welcome-file-list> | |
8 <welcome-file>index.jsp</welcome-file> | |
9 </welcome-file-list> | |
10 | |
11 <!-- Spring 监听器 --> | |
12 <listener> | |
13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |
14 </listener> | |
15 <!-- Spring 配置文件位置 --> | |
16 <context-param> | |
17 <param-name>contextConfigLocation</param-name> | |
18 <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value> | |
19 </context-param> | |
20 | |
21 <!-- 防止 spring 内存溢出监听器 --> | |
22 <listener> | |
23 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> | |
24 </listener> | |
25 | |
26 <!-- openSessionInView 配置 --> | |
27 <filter> | |
28 <filter-name>openSessionInViewFilter</filter-name> | |
29 <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> | |
30 <init-param> | |
31 <param-name>singleSession</param-name> | |
32 <param-value>true</param-value> | |
33 </init-param> | |
34 </filter> | |
35 <filter-mapping> | |
36 <filter-name>openSessionInViewFilter</filter-name> | |
37 <url-pattern>*.action</url-pattern> | |
38 </filter-mapping> | |
39 | |
40 <!-- Struts2 的核心过滤器配置 --> | |
41 <filter> | |
42 <filter-name>struts2</filter-name> | |
43 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> | |
44 </filter> | |
45 <!-- Struts2 过滤器拦截所有的.action 请求 --> | |
46 <filter-mapping> | |
47 <filter-name>struts2</filter-name> | |
48 <url-pattern>*.action</url-pattern> | |
49 </filter-mapping> | |
50 | |
51 <!-- druid 监控页面,使用 ${pageContext.request.contextPath}/druid/index.html 访问 --> | |
52 <servlet> | |
53 <servlet-name>druidStatView</servlet-name> | |
54 <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> | |
55 </servlet> | |
56 <servlet-mapping> | |
57 <servlet-name>druidStatView</servlet-name> | |
58 <url-pattern>/druid/*</url-pattern> | |
59 </servlet-mapping> | |
60 </web-app> |
5.2、编写测试代码
在 TestAction 类中添加一个 saveUser 方法,如下:
1 package me.gacl.action; | |
2 | |
3 import java.util.Date; | |
4 import java.util.UUID; | |
5 | |
6 import me.gacl.model.User; | |
7 import me.gacl.service.UserServiceI; | |
8 | |
9 import org.apache.struts2.convention.annotation.Action; | |
10 import org.apache.struts2.convention.annotation.Namespace; | |
11 import org.apache.struts2.convention.annotation.ParentPackage; | |
12 import org.springframework.beans.factory.annotation.Autowired; | |
13 | |
14 | |
15 //使用 convention-plugin 插件提供的 @Action 注解将一个普通 java 类标注为一个可以处理用户请求的 Action | |
16 //使用 convention-plugin 插件提供的 @Namespace 注解为这个 Action 指定一个命名空间 | |
17 public class TestAction {18 | |
19 /** | |
20 * 注入 userService | |
21 */ | |
22 | |
23 private UserServiceI userService; | |
24 | |
25 /** | |
26 * http://localhost:8080/SSHE/strust2Test!test.action | |
27 * MethodName: test | |
28 * Description: | |
29 * @author xudp | |
30 */ | |
31 public void test(){32 System.out.println("进入 TestAction"); | |
33 userService.test(); | |
34 } | |
35 | |
36 /** | |
37 * http://localhost:8080/SSHE/strust2Test!saveUser.action | |
38 */ | |
39 public void saveUser(){40 User user = new User(); | |
41 user.setId(UUID.randomUUID().toString().replaceAll("-", "")); | |
42 user.setName("xdp 孤傲苍狼"); | |
43 user.setPwd("123456"); | |
44 user.setCreatedatetime(new Date()); | |
45 userService.save(user); | |
46 } | |
47 } |
执行【Maven install】操作,重新编译和发布项目,在执行【Maven install】操作之前,需要修改 TestSpring 这个测试类中的 test 方法的代码,如下:
1 package me.gacl.test; | |
2 | |
3 import me.gacl.service.UserServiceI; | |
4 | |
5 import org.junit.Test; | |
6 import org.springframework.context.ApplicationContext; | |
7 import org.springframework.context.support.ClassPathXmlApplicationContext; | |
8 | |
9 public class TestSpring {10 | |
11 | |
12 public void test(){13 //通过 spring.xml 配置文件创建 Spring 的应用程序上下文环境 | |
14 //ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml"); | |
15 /** | |
16 * 因为已经整合了 Hibernate,UserServiceImpl 类中使用到了 userDao,17 *userDao 是由 spring 创建并且注入给 UserServiceImpl 类的,而 userDao 中又使用到了 sessionFactory 对象 | |
18 * 而创建 sessionFactory 对象时需要使用到 spring-hibernate.xml 这个配置文件中的配置项信息,19 * 所以创建 Spring 的应用程序上下文环境时,需要同时使用 spring.xml 和 spring-hibernate.xml 这两个配置文件 | |
20 * 否则在执行 Maven install 命令时,因为 maven 会先执行 test 方法中的代码,而代码执行到 | |
21 *UserServiceI userService = (UserServiceI) ac.getBean("userService"); | |
22 * 这一行时就会因为 userDao 中使用到 sessionFactory 对象无法正常创建的而出错,这样执行 Maven install 命令编译项目时就会失败!23 * | |
24 */ | |
25 ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"}); | |
26 //从 Spring 的 IOC 容器中获取 bean 对象 | |
27 UserServiceI userService = (UserServiceI) ac.getBean("userService"); | |
28 //执行测试方法 | |
29 userService.test(); | |
30 } | |
31 } |
每次执行【Maven install】命令时都会执行 Junit 单元测试中的代码有时候感觉挺累赘的,有时候往往就是因为一些单元测试中的代码导致【Maven install】命令编译项目失败!
将编译好的项目部署到 tomcat 服务器中运行,输入地址:http://localhost:8080/SSHE/strust2Test!saveUser.action 进行访问,如下所示:
访问 action 的过程中没有出现错误,并且后台也没有报错并且打印出了 Hibernate 执行插入操作时的 SQL 语句,如下所示:
这说明三大框架整合开发的测试通过了。以上就是使用使用 Maven 搭建 Struts2+Spring3+Hibernate4 的整合开发环境的全部内容。
