Spring+Spring MVC+Mybatis+Maven搭建多模块项目

网址:https://blog.csdn.net/kity9420/article/details/53292084 

https://www.2cto.com/kf/201704/621169.html

maven多模块项目SpringMVC简单实例:划分多模块,也就是方便多人开发,自己开发自己的那块没有多大冲突。

项目结构

整个项目目录是这样的:

GitHub地址 :https://github.com/thecattle/maven_model

—- app-parent
|– pom.xml (pom)
|
|– app-dao
| |– pom.xml (jar)
|
|– app-service
| |– pom.xml (jar)
|
|– app-web
|– pom.xml (war)

注意他们的packaging类型,有jar,war和pom三种

dao–作为jar导入–>service–作为jar导入–>web–build成war项目–>运行

项目搭建

父模块仅仅是帮助聚合其他模块构建的工具,它本身并无实际的内容。
父模块与其他模块的目录结构并非一定是父子关系,还可以是平行关系。更改下modules路径即可。

首先new一个maven项目作为parent项目。
pom文件如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

<!--?xml version="1.0" encoding="UTF-8"?-->

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelversion>4.0.0</modelversion>

 

    <groupid>com.sun</groupid>

    maven_test</artifactid>

    <version>1.0-SNAPSHOT</version>

 

    <!--首先在parent中声明打包类型为 pom -->

    <packaging>pom</packaging>

 

    <!-- 声明包含的所有子模块 -->

    <modules>

        <module>maven_service</module>

        <module>maven_web</module>

        <module>maven_dao</module>

    </modules>

 

    <!--不声明会提示警告: Warning:java: 源值1.5已过时, 将在未来所有发行版中删除 -->

    <properties>

        <maven.compiler.source>1.8</maven.compiler.source>

        <maven.compiler.target>1.8</maven.compiler.target>

    </properties>

 

    <dependencies>

 

        <!-- Test -->

        <dependency>

            <groupid>junit</groupid>

            junit</artifactid>

            <version>4.12</version>

            <scope>test</scope>

        </dependency>

 

        <!-- Spring MVC support -->

 

        <dependency>

            <groupid>org.springframework</groupid>

            spring-webmvc</artifactid>

            <version>4.1.4.RELEASE</version>

        </dependency>

 

        <dependency>

            <groupid>org.springframework</groupid>

            spring-web</artifactid>

            <version>4.1.4.RELEASE</version>

        </dependency>

 

    </dependencies>

</project>

再new一个maven项目作为dao模块。pom文件如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!--?xml version="1.0" encoding="UTF-8"?-->

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

 

    <!--要声明父项目-->

    <parent>

        maven_test</artifactid>

        <groupid>com.sun</groupid>

        <version>1.0-SNAPSHOT</version>

    </parent>

 

    <!--打包类型为 jar -->

    <packaging>jar</packaging>

 

    <modelversion>4.0.0</modelversion>

 

    maven_dao</artifactid>

</project>

再new一个maven项目作为service模块,pom如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<!--?xml version="1.0" encoding="UTF-8"?-->

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

 

    <!--要声明父项目-->

    <parent>

        maven_test</artifactid>

        <groupid>com.sun</groupid>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelversion>4.0.0</modelversion>

 

    <!--打包类型为 jar -->

    <packaging>jar</packaging>

 

    maven_service</artifactid>

 

    <dependencies>

 

        <!--将dao模块作为jar包引进来 -->

        <dependency>

            <groupid>com.sun</groupid>

            maven_dao</artifactid>

            <!--版本号为父项目版本号 -->

            <version>${project.version}</version>

        </dependency>

 

    </dependencies>

</project>

再new一个maven项目作为web模块,pom如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<!--?xml version="1.0" encoding="UTF-8"?-->

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

 

    <!--声明父项目-->

    <parent>

        maven_test</artifactid>

        <groupid>com.sun</groupid>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelversion>4.0.0</modelversion>

 

    <!--打包类型为 war -->

    <packaging>war</packaging>

 

    maven_web</artifactid>

 

    <dependencies>

 

        <!-- 引入 service模块 jar -->

        <dependency>

            <groupid>com.sun</groupid>

            maven_service</artifactid>

            <version>${project.version}</version>

        </dependency>

 

    </dependencies>

 

</project>

现在这三个模块就已经关联起来了

配置SpringMVC

在web模块中,web.xml文件如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<!--?xml version="1.0" encoding="UTF-8"?-->

<web-app id="WebApp_ID" version="2.5" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

        <welcome-file>index.html</welcome-file>

    </welcome-file-list>

 

    <!--spring mvc 配置 -->

    <servlet>

        <servlet-name>spring-mvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>classpath:spring-mvc.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup> <!-- 是启动顺序,让这个Servlet随Servletp容器一起启动。 -->

    </servlet>

 

    <servlet-mapping>

        <servlet-name>spring-mvc</servlet-name>

        <url-pattern>/</url-pattern> <!-- 会拦截/的请求。 -->

    </servlet-mapping>

 

</web-app>

spring-mvc.xml:

 

<!--?xml version="1.0" encoding="UTF-8"?-->

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="https://www.springframework.org/schema/context" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

 

    <!--扫描com.sun下的所有包-->

    <context:component-scan base-package="com.sun.**">

 

</context:component-scan></beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值