入门级别的springboot多模块项目搭建
一。创建父工程
1.1File-》New-》Project
1.2 选中Spring Iniaializr,sdk选中1.8
1.3 填写信息及选择java版本,下一步
1.4 其他选项不用勾选,选择所需的Springboot版本,我这里选择2.3.7,接下来点击下一步
1.5 选择项目保存的路径,点击完成
1.6 删除无用的文件夹及文件(.mvn、src、mvnw、mvnw.cmd)
二。创建子工程,先创建公共模块
2.1选中父工程,右键 New-》Module
2.2 选择maven,sdk,点击下一步
2.3 输入项目名danyu-common,点击完成
三。创建子工程(danyu-web)
3.1 过程步骤跟第二步一样
四。修改各类配置文件
4.1 修改父工程(danyu)的pom.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 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | <? 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion >4.0.0</ modelVersion > < packaging >pom</ packaging > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.3.7.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.danyu</ groupId > < artifactId >danyu</ artifactId > < version >0.0.1</ version > < name >danyu</ name > < description >父项目工程</ description > < properties > < danyu.version >1.0.1</ danyu.version > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding > < java.version >1.8</ java.version > < maven-jar-plugin.version >3.1.1</ maven-jar-plugin.version > < druid.version >1.2.2</ druid.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > < exclusions > < exclusion > < groupId >org.junit.vintage</ groupId > < artifactId >junit-vintage-engine</ artifactId > </ exclusion > </ exclusions > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >jstl</ artifactId > </ dependency > <!--阿里数据库连接池 --> < dependency > < groupId >com.alibaba</ groupId > < artifactId >druid-spring-boot-starter</ artifactId > < version >${druid.version}</ version > </ dependency > <!-- Mysql驱动包 --> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > </ dependency > </ dependencies > < modules > < module >danyu-common</ module > < module >danyu-web</ module > </ modules > <!-- <build>--> <!-- <plugins>--> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- </plugin>--> <!-- </plugins>--> <!-- </build>--> < repositories > < repository > < id >public</ id > < name >aliyun nexus</ name > < url >http://maven.aliyun.com/nexus/content/groups/public/</ url > < releases > < enabled >true</ enabled > </ releases > </ repository > </ repositories > < pluginRepositories > < pluginRepository > < id >public</ id > < name >aliyun nexus</ name > < url >http://maven.aliyun.com/nexus/content/groups/public/</ url > < releases > < enabled >true</ enabled > </ releases > < snapshots > < enabled >false</ enabled > </ snapshots > </ pluginRepository > </ pluginRepositories > </ project > |
4.2 修改子工程(danyu-common)的pom.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 26 27 28 29 30 31 32 33 34 35 36 37 38 | <? 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/xsd/maven-4.0.0.xsd"> < parent > < artifactId >danyu</ artifactId > < groupId >com.danyu</ groupId > < version >0.0.1</ version > </ parent > < modelVersion >4.0.0</ modelVersion > < artifactId >danyu-common</ artifactId > < description >公共模块</ description > < dependencies > <!-- servlet包--> < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > </ dependency > <!-- hutool各种工具类 --> < dependency > < groupId >cn.hutool</ groupId > < artifactId >hutool-all</ artifactId > < version >5.5.1</ version > </ dependency > <!-- gson json解析工具类 --> < dependency > < groupId >com.google.code.gson</ groupId > < artifactId >gson</ artifactId > < version >2.8.2</ version > </ dependency > </ dependencies > </ project > |
4.3 修改子工程(danyu-web)的pom.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 26 27 28 29 30 31 32 33 | <? 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/xsd/maven-4.0.0.xsd"> < parent > < artifactId >danyu</ artifactId > < groupId >com.danyu</ groupId > < version >0.0.1</ version > </ parent > < modelVersion >4.0.0</ modelVersion > < artifactId >danyu-web</ artifactId > < description >控制模块</ description > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!-- 通用工具--> < dependency > < groupId >com.zbbz</ groupId > < artifactId >zbbzfw-common</ artifactId > < version >1.0.1</ version > <!-- 该模块的packaging为war,所以下面两项必须设置,否则打包会出出错 --> < type >jar</ type > <!-- <classifier>classes</classifier>--> </ dependency > </ dependencies > </ project > |
五.添加application项目入口类
5.1 在danyu-web中,展开src文件夹,选中java右键,选择Mark Directory as->Sources Root
5.2 新建路径包
5.3 输入包名:com.danyu.web,之后回车完成新建
5.4新建web入口类(DanyuWebApplication.java)
1 2 3 4 5 6 7 8 9 10 11 | package com.danyu.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DanyuWebApplication { public static void main(String[] args) { SpringApplication.run(DanyuWebApplication. class , args); } } |
这一步,如果SpringApplication和SpringBootApplication引入的类包报错,则选中父工程,右键maven-》Reimport,更新重新导入一下
六。添加测试的控制器类
在com.danyu.web下添加包(controller),新建java类(TestController)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.danyu.web.controller; 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; /** * @author danyu * @date 2020/01/01 */ @RestController @RequestMapping ( "/manage" ) public class TestController { @GetMapping ( "/test" ) public String test() { return "访问控制器成功" ; } } |
七。添加springboot配置文件类
7.1 在danyu-web子工程下的resources文件夹,选中右键New-》FIle
输入文件名application.yml
打开application.yml文件,输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 开发环境配置 server: # 服务器的HTTP端口,默认为8080 port: 8080 servlet: # 应用的访问路径 context-path: /danyu spring: datasource: name: test url: jdbc:mysql://localhost:3306/test username: root password: root |
八。启动工程,进行测试
8.1 运行DanyuWebApplication类,如果项目没报错, 则代表项目运行成功
8.2 浏览器访问验证,在浏览器地址栏输入:http://localhost:8080/danyu/manage/test
至此,一个简易的springboot多模块项目搭建成功
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义