springboot多模块项目每层放什么pom springboot多模块项目架构

您所在的位置:网站首页 springboot每层做什么 springboot多模块项目每层放什么pom springboot多模块项目架构

springboot多模块项目每层放什么pom springboot多模块项目架构

2024-05-17 11:09| 来源: 网络整理| 查看: 265

首先新建一个springboot父项目

springboot多模块项目每层放什么pom springboot多模块项目架构_java

springboot多模块项目每层放什么pom springboot多模块项目架构_运维_02

springboot多模块项目每层放什么pom springboot多模块项目架构_spring boot_03

这里不选择其他的,直接next就好了,后续需要再添加。

springboot多模块项目每层放什么pom springboot多模块项目架构_spring boot_04

建立完成后项目结构如下,但是这是一个父项目,做版本控制,什么都不需要,所以我们要删除如下的东西。

springboot多模块项目每层放什么pom springboot多模块项目架构_运维_05

选中的全部删除

然后开始建立子模块

注意这里需要选中springboot-dubbo然后右键

springboot多模块项目每层放什么pom springboot多模块项目架构_spring_06

springboot多模块项目每层放什么pom springboot多模块项目架构_spring boot_07

选择其中的quickstart

springboot多模块项目每层放什么pom springboot多模块项目架构_运维_08

继续创建2个module,分别为module和server,至此多模块springboot项目创建完成。建立完成后的项目结构:

springboot多模块项目每层放什么pom springboot多模块项目架构_mysql_09

父pom文件:

4.0.0 pom module server org.springframework.boot spring-boot-starter-parent 2.3.0.RELEASE com.example parent 0.0.1-SNAPSHOT parent Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin 测试

model工程是我们放实体类和xml实现的地方,我们首先创建一个测试实体类user。

springboot多模块项目每层放什么pom springboot多模块项目架构_spring_10

public class User { private String name; private Integer age; //省略get set方法 }

server是我们逻辑处理和controller存放的地方,首先我们在父pom中添加web依赖,即springboot-dubbo的pom:

org.springframework.boot spring-boot-starter-web

然后创建测试用的启动类,及controller,结构如下:

springboot多模块项目每层放什么pom springboot多模块项目架构_运维_11

SpringbootApplication:

@SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } } @Controller @RequestMapping("user") public class HelloController { @RequestMapping("/printUser") @ResponseBody public User printUser(User user) { return user; } }

这里可以我们会报错,引入不了User这个类,因为我们要设置server依赖于工程model,server工程右键

springboot多模块项目每层放什么pom springboot多模块项目架构_运维_12

springboot多模块项目每层放什么pom springboot多模块项目架构_运维_13

springboot多模块项目每层放什么pom springboot多模块项目架构_mysql_14

然后apply,这时我们就可以导入User这个类了

但是因为我们是maven项目这种设置我们下次启动还是回丢失对Model的引用,所以我们需要在pom中引入对model的依赖 在server pom中添加如下依赖(可以根据提示,自动导入也是可以的)

com.example module 0.0.1-SNAPSHOT compile

然后我们开始启动测试,启动 SpringbootApplication,然后看见控制台输出:

springboot多模块项目每层放什么pom springboot多模块项目架构_java_15

可以看到端口号配置生效

然后再浏览器访问:http://localhost:8083/user/printUsername=‘你好’&age=666

浏览器显示:

springboot多模块项目每层放什么pom springboot多模块项目架构_java_16

至此多模块springboot项目构建完成。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3