基于Maven的SpringSpringMVCMybatis的环境搭建.docx
老大想要做一个TimeSheet的东西,要管理公司员工每年的时间和effort. 反正还要个什么报表,老大发话了,很荣幸我有此机会又可以和刚进公司带我的老大一起做东西了,感觉很好。不过这个项目是一个partime job,自己在工作中抽时间来做。当然啦,堆码的事情肯定我来搞定啦。 项目开发,需求不是我定,所以在等待需求的时候,我就将环境先搭建起来。上次做了一个Spring + Spring MVC + Mybatis + Log4J + JUnit Test的环境搭建,这次也差不多,不过就是基于了Maven,同时也添加了事务管理。 首先,可以去了解一下maven,个人觉得这个东西就是JAR包管理方便,不用自己一个个去写,直接添加dependence就行了,不过有时候要注意哟,有可能重复的包含了某个JAR包,会报冲突的哟,亲!因为只有一个POM文件里指定JAR包,所以整个项目都很小的,一般就几MB,只有你在将该项目打成WAR包的时候,maven会自动的把三方JAR包放进项目,然后你就可以去部署到Tomcat的webapp下面了,放进去,重启Tomcat就OK。 1.开始搭建我的环境吧。首先,IDE我用的是eclipse的JEE版本。要保证装上了Maven哟,可以使用eclipse的Install New Software,URL地址是http:/m2eclipse.sonatype.org/sites/m2e/,然后一步步走下去,安装好了过了会重启一次。给一个忠告哟,安装好了maven后,不要使用IDE里面的了,而是去官方下载一个,解压,然后让IDE的Maven指向你下载安装的,如图: 2.maven安装成功了,我们用Maven创建一个新的WEB项目。File> New> Other>Maven Project.选默认,下一步。在Select an Archetype里面种找到如下图所示 3.然后next,输入groupId:net.yuanmomo;artifactId:Test,然后finish,稍微等待一下,就创建好了。下面的是创建好了的一个初始的项目: 4.但是这个并不是默认的maven项目的标准结构.我们需要进行一些改动. (1).在项目下创建一个Source Folder. Folder Name:src/main/java 如下图所示:(当然,还可以添加用于junit测试的src/test/java和src/test/resources 不过在此就不添加了.)(2).在src/main/java中创建3个包(maven标准推荐是3层包结构)(3).如果装的JDK是1.6的,更改JRE System Libary为1.6版本的.(4).就src/main/webapp文件夹看做一个的WebRoot,然后创建js,css,jsp等文件夹。这样.一个标准的maven web项目创建成功. 5.大家一看上面的项目结构,很熟悉吧。maven的项目结构路径有点讨厌,有点让人很烦。maven打WAR包的时候提取的东西是src/main/webapp下面的文件,然后打成一个WAR的WEB项目就可以部署了,但是,但是,我们在开发的时候,在使用eclipse里面的Tomcat调试的时候,tomcat却不是把src/main/webapp下面的文件部署到服务器。此时,如果你打开Server,然后add and remove,你在左边是找不到Test项目的,如下:原因是因为Tomcat默认回去Test项目的路径下面去找WebRoot>WEB-INF->web.xml(WebRoot文件夹的名字可以不一样,可以使其它的,但是WEB-INF->web.xml这两个名字不能变),但是当前就找不到这几个目录。这个时候有两个解决办法:(5.1). 在Test项目的路径下面,按照路径规则创建WebRoot,WEB-INF等文件夹,然后把上面的web.xml拷贝到新建的WEB-INF中去。放心吧,我上一个项目就是这样做的,不过这种方式打死都不可取的。你修改JSP的时候改的是下面的,maven打包的时候用的上面的。你还得拷贝过来,拷过去,麻烦,如果再有svn管理,累死你,晕死你,我们经常是调试编写的时候再下面做,最后提交的时候还得一个个拷贝上去,还出很多问题。所以建议采用第二种方式。(5.2).转换该项目:(1)在Test工程名上右键->Properties->Project Facets->Convet to faceted form,(2)勾上Dynamic Web Module,选择2.4版本。同时点击下方的further configuration available(3)将默认的WebContent改成src/main/webapp(4)一路点OK.此时在server视图中的tomcat server上点右键->Add and Remove,就能看到该项目于,然后部署啦。至此,我们已经创建好了一个合格的基于Maven的项目,也可以在Tomcat中部署。接下来,我们开始整合我们的J2EE框架。6.先给出数据库的创建脚本,方便测试搭建是否成功。SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"- - 数据库: timesheet - CREATE DATABASE timesheet DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; USE timesheet;- - - 表的结构 testtable -CREATE TABLE IF NOT EXISTS testtable ( id int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(50) COLLATE utf8_bin DEFAULT NULL, date timestamp NULL DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;7.在pom文件中添加必要的jar包的dependence,可以从两个地方查询:http:/search.maven.org/添加后的pom文件如下:<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>net.yuanmomo</groupId> <artifactId>Test</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Test Maven Webapp</name> <url>http:/maven.apache.org</url> <!- 设置 Spring 的版本 -> <properties> <org.springframework.version>3.0.0.RELEASE</org.springframework.version> </properties> <dependencies> <!- 此处开始就是Spring 所有的jar了,spring3.0的jar包拆分了,所以很多 -> <!- Core utilities used by other modules. Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>$org.springframework.version</version> </dependency> <!- Expression Language (depends on spring-core) Define this if you use Spring Expression APIs (org.springframework.expression.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>$org.springframework.version</version> </dependency> <!- Bean Factory and JavaBeans utilities (depends on spring-core) Define this if you use Spring Bean APIs (org.springframework.beans.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>$org.springframework.version</version> </dependency> <!- Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>$org.springframework.version</version> </dependency> <!- Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans) This is the central artifact for Spring's Dependency Injection Container and is generally always defined -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>$org.springframework.version</version> </dependency> <!- Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration Define this if you need any of these integrations -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>$org.springframework.version</version> </dependency> <!- Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context) Define this if you use Spring Transactions or DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>$org.springframework.version</version> </dependency> <!- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>$org.springframework.version</version> </dependency> <!- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you need ORM (org.springframework.orm.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>$org.springframework.version</version> </dependency> <!- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, spring-context) Define this if you need OXM (org.springframework.oxm.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>$org.springframework.version</version> </dependency> <!- Web application development utilities applicable to both Servlet and Portlet Environments (depends on spring-core, spring-beans, spring-context) Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>$org.springframework.version</version> </dependency> <!- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>$org.springframework.version</version> </dependency> <!- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*) -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>$org.springframework.version</version> </dependency> <!- Support for testing Spring applications with tools such as JUnit and TestNG This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs -> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>$org.springframework.version</version> <scope>test</scope> </dependency> <!- 上面的都是Spring的jar包,可以选择性的添加 -> <!- Mybatis 开发包 -> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <!- Mybatis 和Spring的 整合包,是mybatis出的-> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <!- tomcat servlet开发包 -> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!- JSTL标签库 -> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <!- mysql的数据库驱动包 -> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!- 日志打印 log4j包 -> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>runtime</scope> </dependency> <!- 下面两个包 commons-dbcp,commons-pool 是配置数据源的包-> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.4</version> </dependency> <!- 日志记录依赖包,很多都依赖此包,像log4j,json-lib等等 -> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging-api</artifactId> <version>1.1</version> </dependency> <!- Spring 文件上传的包 -> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <!- Spring 文件上传的依赖包 -> <dependency> <groupId>mons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <!- dom4j 解析 XML文件的包 -> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!- 下面的三个包是在配置事务的时候用到的 spring的依赖包 -> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.2.2</version> </dependency> <!- JSON lib 开发包 以及它的依赖包 -> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <classifier>jdk15</classifier> <version>2.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.5</version> </dependency> <!- junit 测试包 -> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>Test</finalName> </build> </project>修改pom文件后,保存,maven会自动的去download这些jar包到本地。等待的时间可能会很长。上面我把所有我这次项目用到的包都罗列出来了,如果你用不到的,可以自行删除。下载完成过后,点开项目左侧的Maven Dependencies,你就能看到很多的下载的JAR包了。8. 修改web.xml,配置Spring,spring MVC,log4j,乱码处理等。<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance" xmlns=" xmlns:web=" xsi:schemaLocation="http:/java