前言

这篇文章简单记录一下 Spring Boot 框架内的项目结构。本来想把这部分写在笔记里,但是有时候想找的时候很难找,所以把它搬这里来吧,方便随时能看。

了解 MVC 架构

MVC 的本质是架构分层的思想,将应用拆分为 Model、View、Controller 三层,实现了关注点分离,提高代码的可维护性。属于通用的架构思想。

组件 核心职责 典型内容 不包含的工作
Model 管理数据、状态与业务逻辑 领域对象、实体类、算法、与数据库/外部服务的交互(如 DAO、Service) 不包含任何界面元素
View 负责数据的展示与 UI 布局 JSP、Thymeleaf、HTML/CSS、前端页面、图形界面 不编写业务逻辑,只显示来自 Model 的数据
Controller 接收用户输入,协调 Model 与 View Servlet、@Controller 类、路由处理方法 不直接操作数据库,不负责最终渲染

说起来个人感觉和“前端 + 后端 + 数据库”差不多,但是又说数据库不属于任何层,只是 Model 层所依赖的持久化基础设施…有点不太明白…那意思就是 Model 层不止包含数据库喽 (?′⌒`)

Spring Boot 项目结构

Spring MVC 是 MVC 架构设计模式的具体,而 Spring Boot 则是在 Spring(含 Spring MVC)基础上做自动化封装的”脚手架”实现框架,让开发者免于繁琐配置地直接使用它。MVC 中 View 的部分现在基本已经被前端分离出去了,而 Spring Boot 基本只负责 MVC 中的 “MC”。这里是 Spring Boot 项目结构:

  • my-springboot-project
    • src
      • main
        • java
          • com
            • example
              • myproject
                • MyProjectApplication.java <– 启动类
                • controller <– 控制层
                • service <– 业务层
                • repository <– 持久层
                • entity <– 实体类
                • config <– 配置类
                • util <– 工具类
        • resources
          • static <– 静态资源 (js, css, images)
          • templates <– 模板文件
          • application.properties <– 核心配置文件
          • mapper <– MyBatis XML文件 (可选)
      • test
        • java <– 测试代码
          • com
            • example
              • myproject
    • target <– 编译输出目录
    • .gitignore
    • pom.xml
    • readme.md

启动类

控制项目启动

特征: 标注 @SpringBootApplication 注解。

1
2
3
4
5
6
7
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
// 这行代码一执行,Spring Boot 项目就启动了
SpringApplication.run(MyApplication.class, args);
}
}

控制类 (Controller)

处理 HTTP 请求,调用 Service,不处理复杂逻辑

特征: 标注 @RestController 或 @Controller。

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
@RequestMapping("/users") // 定义接口路径前缀
public class UserController {

@Autowired
private UserService userService; // 依赖业务层

@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
// 控制层不处理复杂逻辑,只负责转发
return userService.findById(id);
}
}

业务层 (Service)

处理具体的业务逻辑,可被 Controller 调用

特征: 标注 @Service。

1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class UserService {

@Autowired
private UserRepository userRepository; // 依赖持久层

public User findById(Long id) {
// 这里可以写复杂的业务逻辑
// 比如:如果用户不存在,抛出异常,或者创建一个默认用户
return userRepository.findById(id).orElse(null);
}
}

持久层 (repository)

与数据库打交道

特征: MyBatis 中标注 @Mapper; JPA 中继承 Repository 或 JpaRepository 接口。

1
2
3
4
5
6
7
@Mapper
public interface UserMapper {
// 直接定义数据库操作,对应 SQL 语句
User selectById(Long id);

void insert(User user);
}

实体类

用于承载数据的 Java 对象,通常与数据库表一一对应。

特征: 只有私有属性和 Getter/Setter 方法(或使用 Lombok 的 @Data); 如果是 JPA,会标注 @Entity 和 @Table。

1
2
3
4
5
6
@Data // Lombok注解,自动生成get/set
public class User {
private Long id;
private String username;
private String password;
}

配置类

用于替代传统的 XML 配置文件,定制 Spring 容器的行为。

特征: 标注 @Configuration,方法标注 @Bean。

1
2
3
4
5
6
7
8
9
@Configuration
public class WebConfig {

// 注册一个 Bean 到 Spring 容器中
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

工具类

通用的、与业务逻辑无关的辅助功能类,比如计算器之类的…提供全局静态方法以供调用

特征: 通常是静态方法,私有构造方法。

1
2
3
4
5
6
7
8
public class StringUtils {
// 私有构造方法,防止被实例化
private StringUtils() {}

public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}

结语

这篇文章就这么结束了,其实这部分很早就写了,但是现在才把它从笔记中分出来…也许之后还会进行补充。