1、经过一定的配置之后,每一次执行Maven命令都同时对子模块生效,大大减少重复的工作。
2、将整个项目拆分成若干个子工程,方便管理。
1.整个主工程的打包方式必须是pom,也就是说在主工程的pom.xml中必须这样配置
<packaging>pom</packaging> 复制代码
2.Maven聚合工程示意图:
1、IDEA集成开发工具
2、数据库User表设计:
3、Apache-Maven仓库以及环境
1、首先这是一个将SSM框架的各个包分开成的模块,也就是说,现在的dao,service,controller是三个单独的子模块。
2、上面也提到了,主工程打包方式必须是pom包,那么其他子模块呢?
dao层模块,打包方式为jar包。
service模块,打包方式为jar包。
controller模块,因为需要在Tomcat服务器运行,所以这里打包成war包。
1、首先新建一个Maven项目,这里我采用的是非骨架模式。
依然是创建一个Maven非骨架工程:
创建Model时,会自动给你显示出它的主工程,只需要填写dao层模块的模块名就可
下一步,点击finish即可。
注意:
现在的主工程pom.xml文件会自动的引入这个子Model,同时自动添加主工程的打包方式:
同样的子模块jhPro_dao中也会有依赖于主工程的:
由于groupId和version跟主工程相同,所以子模块就一个artifactId节点。
在这里,我们需要手动指明子模块的打包方式:
打开jhPro_service的pom.xml文件,声明打包方式为jar包
打开jhPro_controller的pom.xml文件,声明打包方式为war包
在聚合项目的主工程pom.xml中引入项目中用到的所有包,进行集中管理,聚合项目的pom文件中的依赖如下:
<?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> 复制代码
注:beans_tx.xml主要配置了事务管理。
由于Controller是运行在无服务上,所以必须先创建web模块,具体步骤如下:
1、点击File->Project Structure
2、点击Models—>选择jhPro_controller模块
3、打开jhPro_controller模块
4、选择jhPro_controller模块,新建Web模块
5、修改Web模块的路径和Web.xml的路径
1、修改Web模块的路径
2、修改web.xml的路径
配置web模块配置成功:
6、写代码和配置文件:
web.xml:
<?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和配置文件:
getu是请求,后面跟参数。响应成功,查询到数据: