1 需求分析
基于 SSM 框架的 CRUD 案例
2 技术选型
框架:SpringMVC+Spring+Mybatis
数据库:MySQL
项目构建工具:Maven
类型都为简单Maven工程
3 项目架构设计
3.1传统项目设计方式
3.2Maven 项目设计方式
- 工程创建
- 父工程的资源构件导入
-
- 1.工程11-parent作为整个项目版本控制项目不书写具体的代码
- 2.此工程必须是一个pom工程
- 3.pluginManagement标签和父项目内的dependencyManagement标签的功能一致,只是声明构件但是不导入
-
<modelVersion>4.0.0</modelVersion>
<groupId>com.sxt</groupId>
<artifactId>11-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 在父工程内控制整个的项目的构件的版本控制和插件设置 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.1.3.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mysql.version>5.1.32</mysql.version>
<slf4j.version>1.6.4</slf4j.version>
<druid.version>1.0.9</druid.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<tomcat.version>2.2</tomcat.version>
</properties>
<!-- jar 包的依赖注入 ,由于该工程是一个父工程, 所以 jar 包在该 pom 文件中只是声明 -->
<dependencyManagement>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Mybatis -->
<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>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- JSP 相关 1.el表达式的GAV坐标 2.Servlet的配置 3. -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<!-- 配置生命周期 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件的开启 -->
<build>
<!-- tomcat 插件,由于子项目不一定每个都是 web 项目,所以该插件只是声明,并未开启 -->
<pluginManagement>
<plugins>
<!-- 配置 Tomcat 插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
- 2.聚合工程的创建
-
- 1.此工程为聚合工程继承11-parent工程
- 2.此工程继承父工程作为整个工程的管理,聚合工程模块
- 3.此工为pom工程
- 4.需要包含的子模块
-
- pom文件
<parent>
<groupId>com.sxt</groupId>
<artifactId>11-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.sxt</groupId>
<artifactId>11-Manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>11-Manager-pojo</module>
<module>11-Manager-service</module>
<module>11-Manager-mapper</module>
<module>11-Manager-controller</module>
</modules>
- 子模块分为:
-
- 1.控制层
- 2.mapper层
- 3.pojo层
- 4.service层
-
- 关系解释:
- 1.按照项目设计的时的模式子模块之间为依赖关系
- 2.控制层为核心层,其他层为辅助层,整合文件的存放位置为控制层(因为在web应用中web.xml文件需要被先加载,并且级联加载Springmvc、Spring、Mybatis的配置文件)
-
-
- 控制层的代码和配置:
-
- pom文件的配置
- 注意依赖是有传递性的同样继承也是有传递性的
- 聚合工程继承父工程,子模块为聚合工程内的模块(继承关系)同样拥有父工程内的构件,只要引用即可
- 配置文件的存放位置
-
<parent>
<groupId>com.sxt</groupId>
<artifactId>11-Manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>11-Manager-controller</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- 注入service构件和Service模块产生依赖的关系 -->
<dependency>
<groupId>com.sxt</groupId>
<artifactId>11-Manager-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- JSP 相关 1.el表达式的GAV坐标 2.Servlet的配置 3. -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<!-- 配置生命周期 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- 配置comcat -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 依赖的传递性最后所有的构件都会被控制层所拥有
- Service层:
- pom文件:
-
- 1.依赖mapper层
-
<parent>
<groupId>com.sxt</groupId>
<artifactId>11-Manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>11-Manager-service</artifactId>
<dependencies>
<!--注入mapper构件 产生依赖关系 -->
<dependency>
<groupId>com.sxt</groupId>
<artifactId>11-Manager-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 注入Spring构件-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies>
</project>
- Mapper层
- 需要和pojo层产生关系
-
- 1.注意点项目进行打包处理的时候默认只打包resurces下的配置文件,所以要在Mapper模块的pom文件内配置资源拷贝的设置,不然在项目运行的时候出现mapper映射文件找不到情况。(注解开发则忽略)
-
<parent>
<groupId>com.sxt</groupId>
<artifactId>11-Manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>11-Manager-mapper</artifactId>
<dependencies>
<!-- 注入pojo构件 -->
<dependency>
<groupId>com.sxt</groupId>
<artifactId>11-Manager-pojo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>