项目的拆分与聚合

1.问题

2.思路

1.子工程可以继承父工程,天生继承父功能坐标

2.拆分和聚合:

拆分: 将一个工程拆分成若干个子工程
聚合: 将若干个子工程,聚合到一起运行

3.坐标冲突

排除标签,来排除低版本或者其他坐标

  • 父工程
    • 1.打包方式必须是pom 父工程有pom还可以聚合
    • 2.父工程不写代码 ,src 文件夹删除
    • 3.创建子工程,modules 标签中的内容就是子工程 ---> modules 就是孩子

3.关于高低版本依赖问题

1.产生原因

  • 导入的坐标,有冲突,版本可能不同,导致工程无法运行或找不到某些类
  • 导入两个坐标进行观察
    spring-aop
    4.3.16.RELEASE
    展开当前该坐标所依赖的jar包
    spring-webmvc
    5.2.8.RELEASE

2.解决办法

<dependency>
		<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		<version>4.3.16.RELEASE</version>
		<!--将低版本的坐标或者冲突的坐标进行排除-->
		<exclusions>
			<exclusion>
			<!--不用配置版本号-->
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				</exclusion>
				<exclusion>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

4.项目拆分的简单实现

4.1 创建工程布局

4.2 导入依赖

1.maven_parent

  <packaging>pom</packaging>
    <modules>
        <module>maven_domain</module>
        <module>maven_mapper</module>
        <module>maven_service</module>
        <module>maven_controller</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <spring.version>5.2.8.RELEASE</spring.version>
        <packaging>pom</packaging>
    </properties>
    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <!--springmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring-->
        <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>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mysql driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!--jackson-->

        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.11.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.2</version>
        </dependency>
        <!--connection pool-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
        <!--servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--JSTL-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>



        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>


        <!--文件上传组件-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>

        <!--POI:提供对office文档的读写能力-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
    </dependencies>
    </dependencyManagement>

     <build>
             <resources>
                 <resource>
                     <directory>src/main/resources</directory>
                     <includes>
                         <include>**/*.properties</include>
                         <include>**/*.xml</include>
                     </includes>
                     <filtering>false</filtering>
                 </resource>
                 <resource>
                     <directory>src/main/java</directory>
                     <includes>
                         <include>**/*.properties</include>
                         <include>**/*.xml</include>
                     </includes>
                     <filtering>false</filtering>
                 </resource>
             </resources>
             <plugins>
                 <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-compiler-plugin</artifactId>
                     <version>3.8.0</version>
                     <configuration>
                         <source>11</source>
                         <target>11</target>
                         <encoding>UTF-8</encoding>
                     </configuration>
                 </plugin>
             </plugins>
         </build>
</project>

2.domain

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

3.mapper

<dependencies>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <!--mysql driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--connection pool-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.ccut</groupId>
            <artifactId>maven_domain</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

4.service

 <dependencies>
        <!--springmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.ccut</groupId>
            <artifactId>maven_mapper</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

5. controller

<dependencies>
    <!--springmvc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>

    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>

    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>

    </dependency>


    <!--servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>

    </dependency>
    <!--JSTL-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>

    </dependency>


    <!--文件上传组件-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>

    </dependency>

    <!--POI:提供对office文档的读写能力-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
    </dependency>
    <dependency>
      <groupId>cn.ccut</groupId>
      <artifactId>maven_service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!--tomcat7插件-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/</path>
          <port>8081</port>
          <uriEncoding>UTF-8</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

4.3 业务代码

1.domain

@Setter
@Getter
@ToString
public class Activity implements Serializable {
    private String id;
    private String owner;
    private String name;
    private String startDate;
    private String endDate;
    private String cost;
    private String description;
    private String createTime;
    private String createBy;
    private String editTime;
    private String editBy;
    }

2.mapper

public interface ActivityMapper {
    @Select("select * from tbl_activity")
    public List<Activity> findActivityList();
}

3.service

接口

public interface ActivityService {

    /**
     * 之所以能够导入Activity的包
     *      因为dao的坐标将domain的坐标依赖传递了过来
     * @return
     */
    public List<Activity> findActivityList();

}

实现类

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<Activity> findActivityList() {
        System.out.println("123456");
        return activityMapper.findActivityList();
    }
}

4.controller

@Controller
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Activity> findAll(){
        System.out.println("controller execute ....");
        List<Activity> activityList = activityService.findActivityList();
        return activityList;
    }
}

4.4 配置文件管理与拆分

注意,先按照原有运行成功,再想着拆分配置文件

1.maven_controller (applicationContext-web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描控制器、异常处理器-->
    <context:component-scan base-package="cn.ccut.maven.controller"/>
    <import resource="classpath*:applicationContext-service.xml"/>
    <!--声明注解驱动  @controller -->
    <mvc:annotation-driven/>

    <mvc:default-servlet-handler />
</beans>

2.web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--spring的加载监听-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--
        classpath:加载的是当前工程类路径下的配置文件
        classpath*:加载当前工程及其其它工程下类路径的配置文件
            web工程-依赖-service-resources目录
                  applicationContext-web.xml
              service-依赖-dao-resources目录
                  applicationContext-service.xml
                dao-依赖-domain-resources目录
                  applicationContext-dao.xml

        配置文件的匹配规则:
            通配符:
              applicationContext-*.xml
                applicationContext-web.xml
                applicationContext-service.xml
                applicationContext-dao.xml
    -->
    <param-value>classpath*:applicationContext-service.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--
      1.加载spring配置文件的监听器
      2.前端控制器
  -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:applicationContext-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3.applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="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.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--service-->
    <!--声明组件扫描器-->
    <context:component-scan base-package="cn.ccut.maven.service" />

    <!--加载dao的配置文件-->
    <import resource="classpath*:applicationContext-mapper.xml"/>


    <!--配置声明式事务-->
    <!--<bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
    <!--<property name="dataSource" ref="dataSource"/>-->
    <!--</bean>-->

</beans>

4.applicationContext-mapper.xml


<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="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.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--dao-->
    <!--导入外部配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="url" value="${druid.url}"/>
        <property name="username" value="${druid.username}"/>
        <property name="password" value="${druid.password}"/>
    </bean>

    <!--声明SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="cn.ccut.maven.domain"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--声明MyBatis的扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="cn.ccut.maven.mapper"/>
    </bean>

</beans>

5. mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
      <!--SQL日志-->
      <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

</configuration>

6.druid.properties

druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://127.0.0.1:3306/crm
druid.username=root
druid.password=root

5.运行

maven-->clean install

出现这样的情况,表示构建成功

运行controller

运行成功:

6.Maven的聚合

将多个子模块聚合到一起,然后运行
当把四个子工程聚合合并完成之后,就相当于当前工程已经有了全部的资源

posted @ 2020-09-27 22:38  Tony小哥  阅读(719)  评论(2编辑  收藏  举报