# 项目结构设计

![SpringBoot项目目录结构解析](https://pic1.zhimg.com/v2-2b0ff95afa64a2c0d6764d525859010b_1440w.jpg?source=172ae18b)

![img](https://pic2.zhimg.com/v2-e1065125916752301c899ff1c95c76b9_r.jpg)

**一、项目目录**

![img](https://pic3.zhimg.com/v2-2193dc1756dbed9f73d04f2d91fcec76_r.jpg)

**（一）主要目录**

| 目录名称     | 相对路径               | 主要用途                   | 国外称呼                 |
| -------- | ------------------ | ---------------------- | -------------------- |
| 源码目录     | src\main\java      | 存储源码                   | Source Folders是      |
| 资源目录     | src\main\resources | 存储静态资源、动态页面、配置文件       | Resource Folders     |
| 资源目录(传统) | src\main\webapp    | 作用同上(非必须,部署War包才需要此目录) | Web Resource Folders |
| 测试目录     | src\test\java      | 存储单元测试                 | Test Source Folders  |
| 目标输出     | $output            | 存储编译文件                 | Test Source Folders  |

SpringBoot 官方推荐我们使用轻量级的Jar File 格式来打包和部署工程，如果想部署War File的传统Web项目，则必须添加webapp目录，和进行相关初始化才能正常使用

**（二）git忽略文件配置**

![img](https://pic1.zhimg.com/v2-6ba252e6cf5565000c38d0faae572194_r.jpg)

配置gitignore文件实现对远程仓库的管理

具体配置过程可以参考<https://zhuanlan.zhihu.com/p/104365543>

**（四）\*.iml文件**

这是记录项目目录、项目的maven、所有的依赖以及相应的版本号的文件

此文件为idea的工程文件和maven文件，开发的时候不会提交到服务器。

**（五）help.md**

这个是个帮助文档

**（六）编译文件存放的目录target**

项目编译后自动生成的项目文件，使用maven打包后的文件也会在此处。

**（七）pom.xml**

pom文件为maven工程的主要项目构建文件，以及相关配置文件

**（八）源码目录**

![img](https://pic4.zhimg.com/v2-97dd4290ef5dc6f5749d65f9bfa98c0b_r.jpg)

![img](https://pic4.zhimg.com/v2-d7b52737dbe7befbd9d756d886f86ff3_r.jpg)

1、main源码目录

java目录这个是进行编程和开发的主要目录，业务逻辑代码在这里完成\
DemoApplication.java 入门口类\
在生成的springboot项目中有一个入口类，需要添加注解@SpringBootApplication，此注解标识此类为一个springboot项目启动类。注意包的使用，springboot默认注解会扫描此启动类目录开始及下面的所有子类，如果其他类文件不在此目录下需要在启动类中加入@ComponentScan(basePackages = { "com.xiong.test" }) 注解进行定制扫描。

resources目录这个是资源放置目录：\
1、static 可以存放html或js css等静态文件\
2、template 存放页面渲染模板文件\
3、yml或properties为属性配置文件\
（默认使用resources下面的application.properties文件或 application.yml 文件）

使用yaml文件\
默认加载application.yml 文件，在实际的开发生成生成环境中可以使用多个yml文配置不同的参数，在application.yml 中使用配置：

spring:\
profiles:\
active: dev\
指定对应的属性配置文件。默认匹配的是application-\*.yml 文件，上述示例会查找application-dev.yml 文件

自定义属性文件加载\
自定义yaml文件的加载方式，可以使用spring提供的@ConfigurationProperties( prefix = "minfo" )注解进行添加，prefix为yaml文件的根节点名称，此文件为一个yaml文件，这样可以将此java文件中的属性名称和yml文件的key值相对应。

有如下的yaml自定义配置：

server:\
port: 8099\
context-path: /bootstart

minfo:\
age: 18\
name: alun\
将age和name属性加载到我们的项目中，有两种方式：

方式一：使用注解加载

@Component\
@ConfigurationProperties( prefix = "minfo" )\
public class ManInfoProperties {\
public String getAge() {\
return age;\
}\
public void setAge(String age) {\
this.age = age;\
}\
public String getName() {\
return name;\
}\
public void setName(String name) {\
this.name = name;\
}

private String age;\
private String name;\
}\
方式二：使用@Value 注解

在使用的类中直接加入注解注入方式，获取对应的值

@Value("${minfo.age}")\
private String age;

小结：要点

1、总结有几个目录是本地自动生成的，一般开源的项目中也不会上传的文件

![img](https://pic3.zhimg.com/v2-381b9695aa9300dfa19d34ebc9b71f1e_r.jpg)

2、静态资源目录：\
src/main/resources/META-INF/resources\
src/main/resources/static （推荐）\
src/main/resources/public\
注：若不同静态目录含有相同路径图片，则按上述优先级，即META-INF/resources目录优先级最高。\
<http://localhost:8080/img/test.jpg> （在static目录或者其他目录，来检验目录的优先级）

3、配置文件：application.properties 或者 application.yml\
<https://www.jb51.net/article/151980.htm>

4、pom.xml

5、项目目录

6、前后端交互

7、项目部署 打包 jar war <https://zhuanlan.zhihu.com/p/149736921\\>
docker

8、自动装配原理 注解

**二、项目目录的构建**

**（一）代码层的结构**

根目录：cn.trasen

1.工程启动类：systemapplication

2.实体类(entity)：com.springboot.entity

3.数据访问层(Dao)：com.springboot.repository

4.数据服务层(Service)：系统的接口及其实现类(Impl)

5.前端控制器(Controller)：前端直接访问的接口

6.工具类(utils)：com.springboot.utils

7.常量接口类(constant)：com.springboot.constant

8.配置信息类(config)：com.springboot.config

9.数据传输类(vo)：com.springboot.vo

**（二）资源文件的结构**

根目录:src/main/resources

1.配置文件(.properties/.json等)：config文件夹下

2.国际化(i18n))：i18n文件夹下

3.spring.xml：META-INF/spring文件夹下

4.页面以及js/css/image等：static文件夹下的各自文件下

目录构建参考自：<https://blog.csdn.net/u012675150/article/details/79351990>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qiangrens-organization.gitbook.io/qkd90/kai-fa-kuang-jia-he-gong-ju/xiang-mu-jie-gou-she-ji.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
