l 分析
l 分模塊開發細節注意
注意:表現層工程web.xml去加載Spring applicationContext開頭的配置文件時,我們要使用classpath*
l 理解繼承和聚合
通常繼承和聚合同時使用
父工程的特徵:packing: pom , (jar/war)
n 何爲繼承?
繼承是爲了消除重複,如果將dao、service、web分開創建獨立的工程,則每個工程的pom.xml文件中內容存在重複,比如設置編譯版本、鎖定spring的版本等,可以將這些重複的內容提取出放在父工程的pom.xml中定義
l 何爲聚合?
項目開發通常分模塊/分工程開發,每個模塊開發完成要運行整個工程需要將每個模塊聚合在一起,比如:dao、service、web三個工程最終打成一個war包運行
l 依賴範圍對傳遞依賴的影響(瞭解)
service層依賴於dao層,有一個scope(runtime)
假如dao層依賴於一個jar包A,也有一個scope(test)配置
service能否使用A這個jar?看兩個scope的值
(一)搭建分模塊工程
基於上邊的三個工程分析
繼承:創建一個 parent 工程將所需的依賴都配置在 pom 中
聚合:聚合多個模塊運行。
1.需求
需求描述——將 SSM 工程拆分爲多個模塊開發:
ssm_dao ssm_service ssm_web
2.創建父工程maven-parent
(1)選擇骨架創建父工程
注意代碼所在的路徑
添加項目的打包方式
定義 pom.xml
在父工程的 pom.xml 中抽取一些重複的配置的,比如:鎖定 jar 包的版本、設置編譯版本等。
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
<springmvc.version>5.0.4.RELEASE</springmvc.version> <mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version> </dependency>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId><artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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-aspects</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-test</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins>
</build>
將父工程發佈至倉庫
(2)ssm_dao 子模塊
在父工程上右擊創建 maven 模塊:
選擇“跳過骨架選擇”
填寫模塊名稱
下一步,確定項目的目錄
添加打包方式是 jar
定義 pom.xml
<dependencies>
<!-- Mybatis 和 mybatis 與 spring 的整合 --> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId> <version>1.3.1</version>
</dependency>
<!-- MySql 驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <version>5.1.32</version>
</dependency>
<!-- druid 數據庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- spring 相關 -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>
</dependency>
</dependencies>
只添加到層的 pom,mybatis 和 spring 的整合相關依賴
dao 模塊代碼編寫(省略)
把 dao 模塊 install 到本地倉庫(省略)
(3)ssm_service 子模塊(參考dao)
定義 pom.xml
ssm_service 模塊的 pom.xml 文件中需要繼承父模塊,ssm_service 依賴 ssm_dao 模塊,添加spring 相關的依賴:
<dependencies>
<!-- spring 相關 -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>
</dependency>
<!-- dao 層的依賴 -->
<dependency>
<groupId>com.mavenTest.ssm_mavenTestt</groupId>
<artifactId>ssm_dao</artifactId>
<version>1.0-SNAPSHOT</version></dependency>
</dependencies>
把 service 模塊 install 到本地倉庫(省略)
(4)ssm_web 子模塊
選擇骨架創建 web 子模塊
定義模塊名稱,下一步確認使用自己的本地倉庫
使用骨架創建 web 項目會花費些時間,請耐心等待
創建 java 和 resources 文件夾,轉成 source root
定義 pom.xml
ssm_web 模塊的 pom.xml 文件中需要繼承父模塊,ssm_web 依賴 ssm_service 模塊,和springmvc 的依賴
<dependencies>
<!-- 依賴 service -->
<dependency>
<groupId>com.mavenTest.ssm_mavenTestt</groupId>
<artifactId>ssm_service</artifactId> <version>1.0-SNAPSHOT</version>
</dependency>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
controller代碼編寫和配置文件(省略)
注意 web.xml 中的配置:使用通配符加載
(5)運行調試
方法 1:在 ssm_web 工程的 pom.xml 中配置 tomcat 插件運行
運行 ssm_web 工程它會從本地倉庫下載依賴的 jar 包,所以當 ssm_web 依賴的 jar 包內容修改了必須及時發佈到本地倉庫,比如:ssm_web 依賴的 ssm_service 修改了,需要及時將ssm_service 發佈到本地倉庫。
方法 2:在父工程的 pom.xml 中配置 tomcat 插件運行,自動聚合並執行
推薦方法 2,如果子工程都在本地,採用方法 2 則不需要子工程修改就立即發佈到本地倉庫,父工程會自動聚合並使用最新代碼執行。
注意:如果子工程和父工程中都配置了 tomcat 插件,運行的端口和路徑以子工程爲準。
3.分模塊構建工程-依賴整合
每個模塊都需要 spring 或者 junit 的 jar,況且最終 package 打完包最後生成的項目中的
jar 就是各個模塊依賴的整合,所以我們可以把項目中所需的依賴都可以放到父工程中,模塊
中只留模塊和模塊之間的依賴,那父工程的 pom.xml 可以如下配置:
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
<springmvc.version>5.0.2.RELEASE</springmvc.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- springMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springmvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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-aspects</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId><groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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-aspects</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId></dependency>
<!-- spring 相關 -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>
</dependency>
<!-- spring 相關 事務相關 -->
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>
</dependency>
<!-- junit 測試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version><scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version>
</plugin>
</plugins>
</build>