构建Maven多模块项目+SSM框架整合(一)

  1. Maven

Apache Maven is a software project management and comprehension tool.
Based on the concept of a project object model (POM), Maven can manage
a project’s build, reporting and documentation from a central piece of
information.

Maven就是一个不需要手动导入依赖、项目打包和项目构建的工具
Maven中重要的两个关键词:继承和聚合
继承:作用就是避免配置重复,对于子项目来说应该关心父项目是怎么样配置的。
聚合:字面理解就是“聚在一起合作完成工作”,就是将子模块聚集起来完成相应的项目需求

  1. 为什么要进行模块化开发?

在多人使用Maven协作开发项目时,尤其是稍微上点规模的项目,每个RD的工作都细分到具体功能和模块,有些模块甚至还要单独部署。

我们假设有这样一个商城项目,包括以下几个模块:

商城前台(shop)
管理后台(admin)
数据库交互模块(dao)
通用业务模块(service)
接口模块(api)
通用工具(util)
其中shop和admin需要单独部署,dao、service、util你可能想要一些经验丰富的人来维护,如果使用一个应用来管理的话,所有的功能和模块都会耦合在一起,所有人都可以随意修改代码,这显然不是我们所期望的。

而且使用一个应用来管理的话,任何一个点的代码有变更,整个项目就需要重新build,使用模块化开发的另一个好处是如果dao的代码被修改,只需要重新build dao模块就可以了。web模块可以build成war,dao、service、util等可以build成jar,只需要配置好依赖关系,就可以实现模块间的解耦合。这样的设计才是遵循“高内聚,低耦合”设计原则的。
我们使用上面的例子进行演示,先进行合理的优化,我们希望dao和service作为通用的底层工具来使用,把它们合并成一个核心模块(core),build成core.jar,简单的Maven模块化项目结构如下:

———- mall //顶级项目
|—— pom.xml //packaging = pom
|—— mall-util //通用工具
| |— pom.xml //packaging = jar
|—— mall-core //核心模块
| |— pom.xml //packaging = jar
|—— mall-web-api //接口模块
| |— pom.xml //packaging = war
|—— mall-web-admin//管理后台
| |— pom.xml //packaging = war
|—— mall-web-shop//商城前台
| |— pom.xml //packaging = war
这些模块中api、admin、shop均是可以单独部署的web应用,相互之间没有依赖关系,但都依赖于core模块,而core模块依赖于util模块。接下来我们按照上述确定的结构来搭建项目结构。

  1. 创建项目
    使用开发工具STS,其他工具也大同小异
    (1) new -> Maven project 选择项目放置的地点 和勾选 create a simple project ,命名study
    构建Maven多模块项目+SSM框架整合(一)_第1张图片
    注意:父项目要使用 pom 方式进行packaging, 有三种 默认是 jar ,还有一种是web项目的war
    构建Maven多模块项目+SSM框架整合(一)_第2张图片

    (2)父项目建立好之后,开始建立子模块 new 一个 Maven module
    构建Maven多模块项目+SSM框架整合(一)_第3张图片
    命名为study-dao,parent为 study
    构建Maven多模块项目+SSM框架整合(一)_第4张图片
    选择quickstart
    构建Maven多模块项目+SSM框架整合(一)_第5张图片
    groupID 要和父项目一致,点击finish
    构建Maven多模块项目+SSM框架整合(一)_第6张图片
    查看一下study-dao的pom.xml
    构建Maven多模块项目+SSM框架整合(一)_第7张图片
    查看一下study的pom.xml
    构建Maven多模块项目+SSM框架整合(一)_第8张图片

    (3)同上一样的方式建立 study-service
    只是注意 study-service 模块需要引入 study-dao,在study-service模块的pom.xml 文件中加上
    spring-context也需要用到,先加上

<dependencies>
        <dependency>
            <groupId>com.studygroupId>
            <artifactId>study-daoartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.3.12.RELEASEversion>
        dependency>

    dependencies>

(4)建立 study-web 模块同上有一点不同,就是他是webapp ,需要打包成war
构建Maven多模块项目+SSM框架整合(一)_第9张图片
刚建立的时候会有报错,不要紧,加上 run server就可以
构建Maven多模块项目+SSM框架整合(一)_第10张图片
构建Maven多模块项目+SSM框架整合(一)_第11张图片

看一眼整个项目的目录结构,整体架子搭建起来了
构建Maven多模块项目+SSM框架整合(一)_第12张图片

study-web的pom.xml 添加依赖, 整合SSM框架


<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>com.studygroupId>
        <artifactId>studyartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <groupId>com.studygroupId>
    <artifactId>study-webartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <name>study-web Maven Webappname>
    <url>http://maven.apache.orgurl>
    <properties>
        <spring.version>4.3.12.RELEASEspring.version>
        <servlet.version>3.1.0servlet.version>
        <commons-codec>1.10commons-codec>
        <mybatis.version>3.4.4mybatis.version>
        <mybatis-spring>1.3.0mybatis-spring>
        <c3p0>0.9.5.2c3p0>
        <commons-logging>1.1commons-logging>
        <mysql-connector-java>5.1.41mysql-connector-java>
        <mybatis-generator-core>1.3.2mybatis-generator-core>
        <pagehelper>5.0.0pagehelper>
        <jackson>2.9.0jackson>
        <jstl>1.2jstl>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.studygroupId>
            <artifactId>study-daoartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>com.studygroupId>
            <artifactId>study-serviceartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>${servlet.version}version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
            <version>${commons-codec}version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>${mybatis-spring}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>${spring.version}version>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>${commons-logging}version>
        dependency>

        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>${c3p0}version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql-connector-java}version>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>${mybatis-generator-core}version>
        dependency>

        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>${pagehelper}version>
        dependency>

        
        


        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>${jackson}version>
        dependency>



        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>${jstl}version>
        dependency>


        
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2version>
            <scope>providedscope>
        dependency>

    dependencies>
    <build>
        <finalName>study-webfinalName>
    build>
project>

(5)study-web 的web.xml 的配置



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
    

    <context-param>  
        <param-name>log4jConfigLocationparam-name>  
        <param-value>classpath:log4j.propertiesparam-value>  
    context-param>  

    <context-param>  
        <param-name>log4jRefreshIntervalparam-name>  
        <param-value>60000param-value>  
    context-param>  
    <listener>  
        <listener-class>  
            org.springframework.web.util.Log4jConfigListener  
        listener-class>  
    listener>  
  



    
    <servlet>
        <servlet-name>springservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xml;classpath:applicationContext.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>springservlet-name>
        <url-pattern>/*url-pattern>
    servlet-mapping>

web-app>

log4j.properties的配置

log4j.rootLogger=INFO,Console,File  
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  

#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55  
log4j.appender.File.File = logs/study.log  
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F  
log4j.appender.File.MaxFileSize = 10MB  
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7  
log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

     <mvc:annotation-driven/>
    
    <context:property-placeholder location="classpath:mysql.properties" />

    
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="driverClass" value="${driverClass}">property>
        <property name="jdbcUrl" value="${url}">property>
        <property name="user" value="${user}">property>
        <property name="password" value="${password}">property>
    bean>

    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource">property>
        <property name="configLocation" value="classpath:mybatis.xml">property>
        <property name="mapperLocations" value="classpath:com/study/demo/mapping/*.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
        <property name="basePackage" value="com.study.demo.dao">property>
    bean>

    
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
       <property name="dataSource" ref="dataSource">property>
    bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

beans>

spring-mvc.xml



<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">


    
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8value>
            list>
        property>
    bean>
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
                p:supportedMediaTypes="*/*" />
        mvc:message-converters>
    mvc:annotation-driven>

    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" /> 
            list>
        property>
    bean>


    
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    bean>

    
    

    
    <mvc:annotation-driven />
    <context:component-scan base-package="com.study.demo.dao" />
    
    <context:component-scan base-package="com.study.demo.service.impl" />

    
    <context:component-scan base-package="com.web.controller" />


    
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <context:annotation-config />


beans>  

mysql.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
user=root
password=123456

mybatis.xml



<configuration>
    <settings>
        <setting name="lazyLoadingEnabled" value="true" />
    settings>

configuration>

(5)实现一个简单逻辑,使项目能运行起来
study-dao 模块 单表的简单CRUD,不多说
构建Maven多模块项目+SSM框架整合(一)_第13张图片

study-service 模块 实现逻辑部分
构建Maven多模块项目+SSM框架整合(一)_第14张图片

study-web 模块,写一个Controller,测试下
构建Maven多模块项目+SSM框架整合(一)_第15张图片

ok,都写完了,我们就可以开始运行 study-web项目了,同平常运行非模块的的项目一样,右击study-web项目名,选择run as -> run on server, 选择Tomcat,开始运行
随后访问http://localhost:8080/study-web/aa,得到预期结果
构建Maven多模块项目+SSM框架整合(一)_第16张图片

你可能感兴趣的:(Java)