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

时间:2021-06-21 00:32:48
写在前面的话
  
承接前文《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、如图: Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM 选择创建maven项目,配置JDK,3步中,Create from archetype这个选项是一定要勾选的,不然无法进入下一步,第四步也要注意,选择 org.apache.maven.archetypes:maven-archetype-webapp,因为可能和其他选项相似,一定要看清楚 3、项目命名 Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构,这里,为了和ssm-demo项目区分开来,我们就命名为com.ssm.maven.core。ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称,与maven整合,因此命名为ssm-maven。
Version是项目版本号,idea已经自动生成了。以上三个配置项的命名都可以根据个人习惯或者公司要求来做,是一个较为主观的事情。 
4、maven目录设置
Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
选择已经配置好的maven及目录即可,下面Properties即为上一步里设置的参数。
这里有一个参数需要注意,archetypeCatalog参数,详细说明可以看一下我写的这篇文章《解决新建maven项目速度慢的问题》

5、存储位置设置
Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
项目在本机的存储目录设置完之后,点击Finish即可。

6、mvn生成项目
全部设置成功后,等待mvn将项目架构生成即可,如下,控制台中出现提示信息即生成项目成功。
Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM 初始的项目结构如下: Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
7、代码整合 原项目结构如下: Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM 那么,将原项目src目录下中的java包复制到ssm-maven项目的main目录下,原项目中的配置文件复制到resources文件夹下, mappers文件也复制到resources下,WebRoot中的文件复制到webapp文件夹下,得到如下目录结构的maven项目: Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM 其实,目录结构的差别倒是不大,主要在于pom.xml文件,整个项目的描述文件及相关配置都在此文件中。两个项目的下载地址分别为ssm-demossm-maven,可以下载到本地 对比一下,看一看其中的差异。最明显的差异就是路径的差异,因为要区分两个项目,所以对原先的包名进行重新命名了。到这里,普通JavaWeb项目改造为maven项目就完成了, 可以自己动动手试一下,也可以直接导入源码。
pom文件
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.ssm.maven.core</groupId>
<artifactId>ssm-maven</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm-maven</name>
<url>http://maven.apache.org</url>

<properties>
<!-- 数据库相关版本 -->
<jdbc.driver.version>5.1.25</jdbc.driver.version>
<mybatis.version>3.2.5</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<!-- spring版本 -->
<spring.version>4.2.4.RELEASE</spring.version>
<!-- encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- autoconfig -->
<autoconfig-plugin-version>1.2</autoconfig-plugin-version>
<maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<!-- Begin: 数据库依赖包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${jdbc.driver.version}</version>
<scope>runtime</scope>
</dependency>
<!-- End: 数据库依赖包 -->

<!-- Begin: 日志依赖包 -->
<dependency>
<groupId>org.slf4</groupId>
<artifactId>slf4j-api</artifactId>
<version>20160310</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- End: 日志依赖包 -->

<!-- Begin: aspectj相关jar包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<!-- End: aspectj相关jar包-->

<!-- Begin: spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- End: spring依赖 -->

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>

</dependencies>

<build>
<finalName>ssm-maven</finalName>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<charset>${project.build.sourceEncoding}</charset>
<server>tomcat7</server>
</configuration>
</plugin>

</plugins>
</build>

</project>

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