企业项目开发--企业中的项目架构以及多环境分配(1)

此文已由作者赵计刚授权网易云社区发布。

欢迎访问网易云社区,了解更多网易技术产品运营经验。


1、业务模块与数据模块分离

在实际开发中,我们项目的架构业务模块和数据模块是分离的,举个例子,假设我们的项目有"人员管理模块"和"酒店管理模块"两个模块,按照上一章的介绍,我们会建立下图所示的项目结构:



其中,人员管理模块的controller、service、dao、mapper都在一个项目中,而在实际使用中,我们会将数据模块分离出来,即将以上两个子模块的service、dao、mapper拿出来,放在一个子项目中,形成如下的项目结构:



注意以下几点:

  • 包的命名最好是com.xxx.mapper.user和不是com.xxx.user.mapper,前者在spring.xml中配置mybatis时更方便,具体见spring.xml的中的注释

  • 在controller那一层的项目是需要部署的,即是war,而下边的数据模块是作为war的一个jar,所以在war层的pom.xml需要将下边的数据模块作为一个jar来引入到项目中

  • service层到底是放在业务模块处还是放在数据模块处,这个根据需求而定,一般而言,都放在数据模块处,方便彼此service的调用,如userService调用hotelService,如果这个时候把两个service分别放在各自的业务模块层中,相互的调用就要通过RPC了,当然,有的时候可能有些与其他模块都不调用的service放在war层可能会好一些。

  • 将来编写的缓存模块类、通用模块类、RPC工具类等都会作为jar被war层调用。

 

2、实现

我将上一章的项目做了修改,将ssmm项目改成了userManagement项目,并将userManagement项目实现了业务模块和数据模块的分离,具体的操作参照第一章和第二章的相关内容,这里直接给出项目结构和各个文件。

2.1、项目结构



2.2、代码实现

2.2.1、ssmm0

pom.xml

 

<?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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xxx</groupId>
    <artifactId>ssmm0</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>ssmm0</name>
    <packaging>pom</packaging><!-- 父模块 -->

    <!-- 管理子模块 -->
    <modules>
        <module>userManagement</module><!-- 具体业务1-人员管理系统 -->
        <module>data</module><!-- 封装数据操作 -->
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <!-- dependencyManagement不会引入实际的依赖,只是作为一个依赖池,供其和其子类使用 -->
    <dependencyManagement>
        <dependencies>
            <!-- json -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.1.39</version>
            </dependency>
            <!-- servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>3.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>3.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>3.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>3.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>3.2.6.RELEASE</version>
            </dependency>
            <!-- 这个是使用velocity的必备包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>3.2.6.RELEASE</version>
            </dependency>
            <!-- mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.27</version>
                <scope>runtime</scope>
            </dependency>
            <!-- 数据源 -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
                <version>7.0.47</version>
            </dependency>
            <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!-- velocity -->
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>1.5</version>
            </dependency>
            <dependency>
                <groupId>velocity-tools</groupId>
                <artifactId>velocity-tools-generic</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- 用于加解密 -->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.7</version>
            </dependency>
            <dependency>
                <groupId>org.bouncycastle</groupId>
                <artifactId>bcprov-jdk15on</artifactId>
                <version>1.47</version>
            </dependency>
            <!-- 集合工具类 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.0</version>
            </dependency>
            <!-- http -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.2.6</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <!-- 引入实际依赖 -->
    <dependencies>
        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!-- 集合工具类 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <resources>
            <!-- 
                这里配置了这一块儿true,才可以让指定文件(这里是src/main/resources/*.xml)读到pom.xml中的配置信息 ,
                值得注意的是,如果src/main/resources下还有其他文件,而你不想让其读pom.xml,
                你还必须得把src/main/resources下的其余文件再配置一遍,配置为false(不可读pom.xml),
                如下边的注释那样,否则,会报这些文件(在这里,就是*.properties)找不到的错误
            -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.xml</include>
                </includes>
            </resource>
            <!-- <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>可以读 ,若改为false就是不可读
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource> -->
        </resources>
        
    </build>
    
    <!-- 
        profiles可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果 
        注意两点:
        1)<activeByDefault>true</activeByDefault>这种情况表示服务器启动的时候就采用这一套env(在这里,就是prod)
        2)当我们启动服务器后,想采用开发模式,需切换maven的env为dev,如果env的配置本身就是dev,需要将env换成rc或prod,点击apply,然后再将env切换成dev,点击apply才行
    -->
    <profiles>
        <!-- 开发env -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>env</name>
                    <value>dev</value>
                </property>
            </activation>
            <properties>
                <env>dev</env>

                <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
                <!-- 
                    对于jdbc.url中内容的配置,如果需要配置 &amp;时,有两种方法:
                    1)如下边这样,使用<![CDATA[XXX]]>包起来
                    2)使用jdbc.properties文件来读取此pom.xml,然后spring.xml再读取jdbc.properties文件
                    显然,前者更方便,而且还省了一个jdbc.properties的文件,但是,有的时候,还是会用后者的;
                    在使用后者的时候,注意三点:
                    1)需要修改上边的build中的内容
                    2)需要在spring.xml中配置<context:property-placeholder location="classpath:jdbc.properties"/>
                    3)将jdbc.properties放在ssmm0-data项目中,之后需要将ssmm0-data项目的env配置为dev
                -->
                <jdbc.url><![CDATA[jdbc:mysql://127.0.0.1:3306/blog?zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=utf-8]]></jdbc.url>
                <jdbc.username>root</jdbc.username>
                <jdbc.password>123456</jdbc.password>
            </properties>
        </profile>
        <!-- 预上线env -->
        <profile>
            <id>rc</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>env</name>
                    <value>rc</value>
                </property>
            </activation>
            <properties>
                <env>rc</env>

                <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
                <!-- 假设的一个地址 -->
                <jdbc.url><![CDATA[jdbc:mysql://10.10.10.100:3306/blog?zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=utf-8]]></jdbc.url>
                <jdbc.username>root2</jdbc.username>
                <jdbc.password>1234562</jdbc.password>
            </properties>
        </profile>
        <!-- 线上env -->
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>env</name>
                    <value>prod</value>
                </property>
            </activation>
            <properties>
                <env>prod</env>
                
                <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
                <!-- 假设的一个地址 -->
                <jdbc.url><![CDATA[jdbc:mysql://99.99.99.999:3307/blog?zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=utf-8]]></jdbc.url>
                <jdbc.username>sadhijhqwui</jdbc.username>
                <jdbc.password>zxczkchwihcznk=</jdbc.password>
            </properties>
        </profile>
    </profiles>
</project>

 

注意:

  • 所有的注意点:都在注释中

  • 上述<build>中的resource的配置是为了是spring.xml可以读取pom.xml文件的内容,具体的注意点,查看注释

  • profiles的配置是为了配置多套环境(在这里配置了三套env,开发,预上线和线上环境),具体的注意点,查看注释


免费领取验证码、内容安全、短信发送、直播点播体验包及云服务器等套餐

更多网易技术、产品、运营经验分享请点击


相关文章:
【推荐】 Openwrt 远程调试

posted @ 2018-12-19 16:01  网易数帆  阅读(260)  评论(0编辑  收藏  举报