maven servlet配置_Maven整合SSM

1 需求分析

基于 SSM 框架的 CRUD 案例

2 技术选型

框架:SpringMVC+Spring+Mybatis

数据库:MySQL

项目构建工具:Maven

类型都为简单Maven工程

3 项目架构设计

3.1传统项目设计方式

1ca014c5c8fe76fbd7fd70bc881bf61d.png

3.2Maven 项目设计方式

a7a10396ab09e2579f11007ce6ab828e.png

289a0fbc62e1ffdc0a0adc093e136982.png
  • 工程创建

7e7269bb2024e40a048e698c16a24c95.png
  • 父工程的资源构件导入
      • 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.需要包含的子模块

9032461e546ccfea31bfa2bba4650043.png

10f806611534fc61a4aaf83117fb0870.png
  • 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文件的配置
      • 注意依赖是有传递性的同样继承也是有传递性的
      • 聚合工程继承父工程,子模块为聚合工程内的模块(继承关系)同样拥有父工程内的构件,只要引用即可
      • 配置文件的存放位置

d704e7cb6b11b75fb24ee29265508efc.png
<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>
  • 依赖的传递性最后所有的构件都会被控制层所拥有

9f1a4d82c977549ed4f427f204bb6df4.png

  • 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值