一、通過必定的配置以後,每一次執行Maven命令都同時對子模塊生效,大大減小重複的工做。
二、將整個項目拆分紅若干個子工程,方便管理。php
1.整個主工程的打包方式必須是pom,也就是說在主工程的pom.xml中必須這樣配置前端
<packaging>pom</packaging>
複製代碼
2.Maven聚合工程示意圖:java
一、IDEA集成開發工具
二、數據庫User表設計:mysql
一、首先這是一個將SSM框架的各個包分開成的模塊,也就是說,如今的dao,service,controller是三個單獨的子模塊。
二、上面也提到了,主工程打包方式必須是pom包,那麼其餘子模塊呢?
dao層模塊,打包方式爲jar包。
service模塊,打包方式爲jar包。
controller模塊,由於須要在Tomcat服務器運行,因此這裏打包成war包。git
一、首先新建一個Maven項目,這裏我採用的是非骨架模式。github
注意:
如今的主工程pom.xml文件會自動的引入這個子Model,同時自動添加主工程的打包方式:web
打開jhPro_controller的pom.xml文件,聲明打包方式爲war包spring
在聚合項目的主工程pom.xml中引入項目中用到的全部包,進行集中管理,聚合項目的pom文件中的依賴以下:sql
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wm</groupId>
<artifactId>jhPro</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>jhPro_dao</module>
<module>jhPro_service</module>
<module>jhPro_controller</module>
</modules>
<properties>
<mysql-v>5.1.41</mysql-v>
<spring-v>4.3.6.RELEASE</spring-v>
<c3p0-v>0.9.1</c3p0-v>
<ph-v>5.0.0</ph-v>
<mybatis-v>3.4.2</mybatis-v>
<mybspri-v>1.3.1</mybspri-v>
<jackson-v>2.7.9</jackson-v>
<valid-v>5.4.1.Final</valid-v>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-v}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0-v}</version>
</dependency>
<!--引入pageHelper分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${ph-v}</version>
</dependency>
<!-- MBG -->
<!--MyBatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-v}</version>
</dependency>
<!-- MyBatis整合Spring的適配包 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybspri-v}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-v}</version>
</dependency>
<!-- Spring-Jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-v}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-v}</version>
</dependency>
<!-- 返回json字符串的支持 -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-v}</version>
</dependency>
<!--JSR303數據校驗支持;tomcat7及以上的服務器, tomcat7如下的服務器:el表達式。額外給服務器的lib包中替換新的標準的el -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${valid-v}</version>
</dependency>
<!-- (jstl,junit) -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
複製代碼
注意:子模塊的jar包依賴能夠不添加version,由主工程負責管理版本號。數據庫
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jhPro</artifactId>
<groupId>com.wm</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jhPro_dao</artifactId>
<packaging>jar</packaging>
<dependencies>
<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>c3p0</groupId>
<artifactId>c3p0</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
</dependencies>
</project>
複製代碼
注意:因爲Service層須要引用Dao層的類和對象,因此須要依賴jhPro_dao的模塊。
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jhPro</artifactId>
<groupId>com.wm</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jhPro_service</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.wm</groupId>
<artifactId>jhPro_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
</dependencies>
</project>
複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jhPro</artifactId>
<groupId>com.wm</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jhPro_controller</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.wm</groupId>
<artifactId>jhPro_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
</project>
複製代碼
因爲Controller是運行在無服務上,因此必須先建立web模塊,具體步驟以下:
一、點擊File->Project Structure
二、點擊Models—>選擇jhPro_controller模塊
三、打開jhPro_controller模塊
四、選擇jhPro_controller模塊,新建Web模塊
五、修改Web模塊的路徑和Web.xml的路徑
一、修改Web模塊的路徑
二、修改web.xml的路徑
配置web模塊配置成功:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<!--編碼過濾器 -->
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器以及springmvc配置文件配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 經過監聽器加載spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 設置監聽的配置文件路徑 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans_*.xml</param-value>
</context-param>
</web-app>
複製代碼
java和配置文件:
將打好的war包放到Tomcat服務器中。
war包的路徑:
測試連接:
說明:因爲篇幅過長,若是所有寫上代碼,必定影響你們的閱讀體驗,在這裏我只把主要的代碼和截圖放上去了。依賴的jar包可能有些沒用到,可是後面的項目會使用到,這裏就沒有刪除。源碼地址:github.com/Guanyong-Li…