Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM

时间:2022-05-04
本文章向大家介绍Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM,主要内容包括前言、整合步骤、pom文件、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

前言

  承接前文《Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven》,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普通的JavaWeb项目变为由maven管理的规范化项目,使项目变得简单。如果你已经安装maven并在开发软件中配置好maven后,即可开始体验maven带给你的便利,当然,仅仅一个项目是不可能让你迅速喜欢上maven的,这一篇只是上车而已,慢慢来。

  因为已经有了项目代码,所以新建maven步骤这里可以忽略的看一下,你可以自行下载代码直接导入到工程即可。

  第一阶段余下的文章中所有关于bug修复、功能增加、代码修改都会在此maven项目中进行,原来的项目不会继续更新了。

  项目实际效果展示在这里,账密:admin 123456   下载地址,点这里   github地址,在这里

整合步骤

1、打开编辑器,File -> New -> Project, 新建maven项目。

2、如图:

  选择创建maven项目,配置JDK,第3步中,Create from archetype这个选项是一定要勾选的,不然无法进入下一步,第四步也要注意,选择

org.apache.maven.archetypes:maven-archetype-webapp,因为可能和其他选项相似,一定要看清楚。

3、项目命名

  GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构,为了和ssm-demo项目区分开来,我们就命名为com.ssm.maven.core。

  ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称,与maven整合,因此命名为ssm-maven。

  Version是项目版本号,idea已经自动生成了。

  以上三个配置项的命名都可以根据个人习惯或者公司要求来做,是一个较为主观的事情。 

4、maven目录设置

  选择已经配置好的maven及目录即可,下面Properties即为上一步里设置的参数。   这里有一个参数需要注意,archetypeCatalog参数,详细说明可以看一下我写的这篇文章《解决新建maven项目速度慢的问题》

5、存储位置设置

  项目在本机的存储目录设置完之后,点击Finish即可。

6、mvn生成项目

  全部设置成功后,等待mvn将项目架构生成即可,如下,控制台中出现提示信息即生成项目成功。

  初始的项目结构如下:

7、代码整合

  原项目结构如下:

  那么,将原项目src目录下中的java包复制到ssm-maven项目的main目录下,原项目中的配置文件复制到resources文件夹下,

mappers文件也复制到resources下,WebRoot中的文件复制到webapp文件夹下,得到如下目录结构的maven项目:

  其实,目录结构的差别倒是不大,主要在于pom.xml文件,整个项目的描述文件及相关配置都在此文件中。两个项目的下载地址分别为ssm-demossm-maven,可以下载到本地对比一下,看一看其中的差异。最明显的差异就是路径的差异,因为要区分两个项目,所以对原先的包名进行重新命名了。到这里,普通JavaWeb项目改造为maven项目就完成了,可以自己动动手试一下,也可以直接导入源码。

pom文件

pom.xml配置如下:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.ssm.maven.core</groupId>
  5     <artifactId>ssm-maven</artifactId>
  6     <packaging>war</packaging>
  7     <version>1.0-SNAPSHOT</version>
  8     <name>ssm-maven</name>
  9     <url>http://maven.apache.org</url>
 10 
 11     <properties>
 12         <!-- 数据库相关版本 -->
 13         <jdbc.driver.version>5.1.25</jdbc.driver.version>
 14         <mybatis.version>3.2.5</mybatis.version>
 15         <mybatis-spring.version>1.2.2</mybatis-spring.version>
 16         <!-- spring版本 -->
 17         <spring.version>4.2.4.RELEASE</spring.version>
 18         <!-- encoding -->
 19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 20         <!-- autoconfig -->
 21         <autoconfig-plugin-version>1.2</autoconfig-plugin-version>
 22         <maven.test.skip>true</maven.test.skip>
 23     </properties>
 24 
 25     <dependencies>
 26         <dependency>
 27             <groupId>junit</groupId>
 28             <artifactId>junit</artifactId>
 29             <version>4.9</version>
 30             <scope>test</scope>
 31         </dependency>
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-test</artifactId>
 35             <version>${spring.version}</version>
 36             <scope>test</scope>
 37         </dependency>
 38         <dependency>
 39             <groupId>commons-logging</groupId>
 40             <artifactId>commons-logging</artifactId>
 41             <version>1.1.3</version>
 42         </dependency>
 43         <dependency>
 44             <groupId>commons-collections</groupId>
 45             <artifactId>commons-collections</artifactId>
 46             <version>3.2.1</version>
 47         </dependency>
 48         <dependency>
 49             <groupId>commons-io</groupId>
 50             <artifactId>commons-io</artifactId>
 51             <version>2.4</version>
 52         </dependency>
 53         <dependency>
 54             <groupId>commons-lang</groupId>
 55             <artifactId>commons-lang</artifactId>
 56             <version>2.6</version>
 57         </dependency>
 58 
 59         <!-- Begin: 数据库依赖包 -->
 60         <dependency>
 61             <groupId>org.mybatis</groupId>
 62             <artifactId>mybatis</artifactId>
 63             <version>${mybatis.version}</version>
 64         </dependency>
 65         <dependency>
 66             <groupId>org.mybatis</groupId>
 67             <artifactId>mybatis-spring</artifactId>
 68             <version>${mybatis-spring.version}</version>
 69         </dependency>
 70         <dependency>
 71             <groupId>mysql</groupId>
 72             <artifactId>mysql-connector-java</artifactId>
 73             <version>${jdbc.driver.version}</version>
 74             <scope>runtime</scope>
 75         </dependency>
 76         <!-- End: 数据库依赖包 -->
 77 
 78         <!-- Begin: 日志依赖包 -->
 79         <dependency>
 80             <groupId>org.slf4</groupId>
 81             <artifactId>slf4j-api</artifactId>
 82             <version>20160310</version>
 83         </dependency>
 84         <dependency>
 85             <groupId>org.slf4j</groupId>
 86             <artifactId>slf4j-log4j12</artifactId>
 87             <version>1.7.7</version>
 88         </dependency>
 89         <dependency>
 90             <groupId>log4j</groupId>
 91             <artifactId>log4j</artifactId>
 92             <version>1.2.16</version>
 93         </dependency>
 94         <!-- End: 日志依赖包 -->
 95 
 96         <!-- Begin: aspectj相关jar包-->
 97         <dependency>
 98             <groupId>org.aspectj</groupId>
 99             <artifactId>aspectjrt</artifactId>
100             <version>1.7.4</version>
101         </dependency>
102         <dependency>
103             <groupId>org.aspectj</groupId>
104             <artifactId>aspectjweaver</artifactId>
105             <version>1.7.4</version>
106         </dependency>
107         <!-- End: aspectj相关jar包-->
108 
109         <!-- Begin: spring依赖 -->
110         <dependency>
111             <groupId>org.springframework</groupId>
112             <artifactId>spring-context-support</artifactId>
113             <version>${spring.version}</version>
114         </dependency>
115         <dependency>
116             <groupId>org.springframework</groupId>
117             <artifactId>spring-jdbc</artifactId>
118             <version>${spring.version}</version>
119         </dependency>
120         <dependency>
121             <groupId>org.springframework</groupId>
122             <artifactId>spring-tx</artifactId>
123             <version>${spring.version}</version>
124         </dependency>
125         <dependency>
126             <groupId>org.springframework</groupId>
127             <artifactId>spring-webmvc</artifactId>
128             <version>${spring.version}</version>
129         </dependency>
130         <!-- End: spring依赖 -->
131 
132         <dependency>
133             <groupId>javax.servlet</groupId>
134             <artifactId>javax.servlet-api</artifactId>
135             <version>3.1.0</version>
136             <scope>provided</scope>
137         </dependency>
138         <dependency>
139             <groupId>javax.servlet.jsp</groupId>
140             <artifactId>jsp-api</artifactId>
141             <version>2.2</version>
142         </dependency>
143         <dependency>
144             <groupId>javax.servlet</groupId>
145             <artifactId>jstl</artifactId>
146             <version>1.2</version>
147         </dependency>
148 
149         <dependency>
150             <groupId>net.sf.json-lib</groupId>
151             <artifactId>json-lib</artifactId>
152             <version>2.2.3</version>
153             <classifier>jdk15</classifier>
154         </dependency>
155 
156         <dependency>
157             <groupId>com.fasterxml.jackson.core</groupId>
158             <artifactId>jackson-databind</artifactId>
159             <version>2.5.3</version>
160         </dependency>
161 
162         <dependency>
163             <groupId>com.alibaba</groupId>
164             <artifactId>fastjson</artifactId>
165             <version>1.2.4</version>
166         </dependency>
167 
168         <dependency>
169             <groupId>commons-codec</groupId>
170             <artifactId>commons-codec</artifactId>
171             <version>1.4</version>
172         </dependency>
173 
174     </dependencies>
175 
176     <build>
177         <finalName>ssm-maven</finalName>
178         <plugins>
179 
180             <plugin>
181                 <groupId>org.apache.maven.plugins</groupId>
182                 <artifactId>maven-compiler-plugin</artifactId>
183                 <version>3.2</version>
184                 <configuration>
185                     <source>1.7</source>
186                     <target>1.7</target>
187                 </configuration>
188             </plugin>
189 
190             <plugin>
191                 <groupId>org.apache.tomcat.maven</groupId>
192                 <artifactId>tomcat7-maven-plugin</artifactId>
193                 <version>2.2</version>
194                 <configuration>
195                     <port>8080</port>
196                     <charset>${project.build.sourceEncoding}</charset>
197                     <server>tomcat7</server>
198                 </configuration>
199             </plugin>
200 
201         </plugins>
202     </build>
203 
204 </project>

如果有什么问题的话,留言或者发私信即可。