美文网首页springboot
使用Idea IntelliJ构建maven多模块项目(spri

使用Idea IntelliJ构建maven多模块项目(spri

作者: 侧耳倾听y | 来源:发表于2020-06-20 17:40 被阅读0次

    文章开始前,先说一下,最好在IntelliJ Idea中配置好maven以及把仓库地址配置为国内的,不然的话下载jar包会非常慢

    1.File-New-Project

    选择如下方式:Spring Initializr


    1. 点击NEXT

    注意这里Type要选择Maven POM,这样构建出来的项目没有src文件夹,作为父工程,子模块有什么公共的jar包可以放到父工程的pom文件中,方便管理

    3.一路NEXT到底

    生成文件目录如下图的工程


    4.给父工程pom文件加上spring-boot-starter-web依赖

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

    为什么要加
    Spring Boot提供了许多“Starters”,可以将jar添加到类路径中,spring-boot-starter-parent是一个特殊的启动器,提供有用的Maven默认值,其他“Starters”提供了在开发特定类型的应用程序时可能需要的依赖项。由于我们正在开发Web应用程序,因此我们添加了 spring-boot-starter-web依赖项。
    换句话说,我们加了spring-boot-starter-web依赖之后,可以少写一些依赖。


    我们可以在看一下目前项目的依赖图,红框内的部分,都是spring-boot-starter-web带来的

    5.创建子模块:对着父工程右键-New-Module

    选择maven


    起名为web,父工程为刚开始创建的工程,点击FINISH
    子模块web的pom文件中可以看到父工程

    父工程springboot_demo也可以看到子模块

    6.同样的步骤,创建其他子模块:pojoservice后,查看目前工程的目录结构

    1. 子模块之间的依赖关系:web依赖serviceservice依赖pojo

    8.编写web模块启动类

    再正常不过的springboot启动类


    1. 编写测试类代码

    大体目录结构


    controller层

    package com.example.web;
    
    import com.example.pojo.TestPojo;
    import com.example.service.TestService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        @Autowired
        private TestService testService;
    
        @GetMapping("/getTestData")
        public TestPojo getTestData() {
            return testService.getTestData();
        }
    }
    

    service层

    package com.example.service.impl;
    
    import com.example.pojo.TestPojo;
    import com.example.service.TestService;
    import org.springframework.stereotype.Service;
    
    @Service
    public class TestServiceImpl implements TestService {
    
        @Override
        public TestPojo getTestData() {
            TestPojo pojo = new TestPojo();
            pojo.setTestData("test data : hello world");
            return pojo;
        }
    }
    
    

    pojo层

    package com.example.pojo;
    
    
    public class TestPojo {
    
        private String testData;
    
        public String getTestData() {
            return testData;
        }
    
        public void setTestData(String testData) {
            this.testData = testData;
        }
    }
    
    1. 启动项目
    1. 测试无误

    相关文章

      网友评论

        本文标题:使用Idea IntelliJ构建maven多模块项目(spri

        本文链接:https://www.haomeiwen.com/subject/oirztktx.html