
SpringBoot多模块搭建
打开终端,进入父项目目录,执行以下命令:
发布日期:2021-05-08 09:46:08
浏览次数:24
分类:精选文章
本文共 4465 字,大约阅读时间需要 14 分钟。
Spring Boot 多模块项目搭建指南
项目结构
本项目采用模块化设计,通过Maven管理多个子模块,实现高效的项目维护和管理。项目结构如下:
├── demo-parent│ ├── demo-dao│ │ ├── pom.xml│ │ └── application-dao.properties│ ├── demo-biz│ │ ├── pom.xml│ │ ├── DemoService.java│ │ └── DemoServiceImpl.java│ └── demo-web│ ├── pom.xml│ ├── DemoController.java│ └── application.properties
父项目配置
父项目demo-parent
的pom.xml
配置如下:
4.0.0 jar demo-dao demo-biz demo-web org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE ../ com.example demo 0.0.1-SNAPSHOT
子模块创建
1. Dao模块
pom.xml
demo com.example 0.0.1-SNAPSHOT 4.0.0 demo-dao org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.version} mysql mysql-connector-java ${mysql-connector.version} org.project.lombok lombok ${lombok.version}
应用配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/goods?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootmybatis.mapper-locations=classpath:mybatis/*.xmlmybatis.type-aliases-package=com.demo.dao.entity
2. Biz模块
pom.xml
demo com.example 0.0.1-SNAPSHOT 4.0.0 demo-biz com.example demo-dao ${demo.version}
服务接口与实现
package com.demo.biz.service;public interface DemoService { String test();}
package com.demo.biz.service.impl;import com.demo.biz.service.DemoService;import com.demo.dao.entity.Good;import com.demo.dao.mapper.GoodMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class DemoServiceImpl implements DemoService { @Autowired private GoodMapper goodMapper; public String test() { Good good = goodMapper.selectByPrimaryKey("1"); return good.toString(); }}
3. Web模块
pom.xml
demo com.example 0.0.1-SNAPSHOT 4.0.0 demo-web com.example demo-biz ${demo.version} org.springframework.boot spring-boot-starter-web ${springboot.version}
Web应用配置
package com.demo.web;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(scanBasePackages = "com.demo")@MapperScan("com.demo.dao.mapper")public class DemoWebApplication { public static void main(String[] args) { SpringApplication.run(DemoWebApplication.class, args); }}
Web控制器
package com.demo.web.controller;import com.demo.biz.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("demo")public class DemoController { @Autowired private DemoService demoService; @GetMapping("test") public String test() { return demoService.test(); }}
运行说明
mvn spring-boot:run
- 访问地址:
- http://localhost:8080/demo/test
以上配置文件和项目结构可根据实际需求进行修改和扩展,方便多环境配置管理,确保代码的可维护性和可扩展性。
发表评论
最新留言
很好
[***.229.124.182]2025年04月18日 03时10分15秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
02、MySQL—数据库基本操作
2019-03-05
OpenJDK1.8.0 源码解析————HashMap的实现(一)
2019-03-05
MySQL-时区导致的时间前后端不一致
2019-03-05
2021-04-05阅读小笔记:局部性原理
2019-03-05
go语言简单介绍,增强了解
2019-03-05
架构师入门:搭建基本的Eureka架构(从项目里抽取)
2019-03-05
MongoDB 快速扫盲贴
2019-03-05
one + two = 3
2019-03-05
sctf_2019_easy_heap
2019-03-06
PyQt5之音乐播放器
2019-03-06
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
2019-03-06
SQL注入
2019-03-06
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
2019-03-06
Problem 330A - Cakeminator (思维)
2019-03-06
LeetCode75 颜色分类 (三路快排C++实现与应用)
2019-03-06
C语言+easyX图形库的推箱子实现
2019-03-06
调试vs2019代码的流程
2019-03-06
脱壳与加壳-加壳-6-代码实现加密导入表
2019-03-06
Typora配置PicGo时,提示Failed to fetch
2019-03-06
bcolz的新操作
2019-03-06