项目构建
spring initializr
 打开https://start.spring.io/ 这个网站来生成一个 Spring Boot 的项目。

解压文件到指定目录
unzip HelloWorld.zip -d ~/Documents/SpringBoot-Note
查看目录结构
tree.├── HELP.md├── mvnw├── mvnw.cmd├── pom.xml└── src├── main│ ├── java│ │ └── com│ │ └── github│ │ └── HelloWorld│ │ └── HelloWorldApplication.java│ └── resources│ ├── application.properties│ ├── static│ └── templates└── test└── java└── com└── github└── HelloWorld└── HelloWorldApplicationTests.java14 directories, 7 files
IDEA


工程结构
mvn的工程结构
├── pom.xml└── src├── main│ ├── java│ └── resources└── test└── java
pom配置信息
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.11.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.github</groupId><artifactId>HelloWorld</artifactId><version>0.0.1-SNAPSHOT</version><name>HelloWorld</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
启动项目
启动入口
@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;
package com.spring;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloMainApplication {public static void main(String[] args) {SpringApplication.run(HelloMainApplication.class,args);}}
注意Spring Boot 的启动类位置是需要最外层的,否则可能导致一些类无法被正确扫描到
**
启动方式

命令行模式方式
mvn spring-boot:run
启动端口
pring Boot 项目会使用 8080 作为项目的端口号。如果我们修改端口号的话,直接修改
src/main/resources/application.properties配置文件即可。
server.port=8090
启动日志结果
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.1.11.RELEASE)2019-12-18 16:35:03.898 INFO 25701 --- [ main] c.g.HelloWorld.HelloWorldApplication : Starting HelloWorldApplication on baxiang with PID 25701 (/home/baxiang/Documents/SpringBoot-Note/HelloWorld/target/classes started by baxiang in /home/baxiang/Documents/SpringBoot-Note/HelloWorld)2019-12-18 16:35:03.900 INFO 25701 --- [ main] c.g.HelloWorld.HelloWorldApplication : No active profile set, falling back to default profiles: default2019-12-18 16:35:04.710 INFO 25701 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2019-12-18 16:35:04.743 INFO 25701 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2019-12-18 16:35:04.743 INFO 25701 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]2019-12-18 16:35:04.829 INFO 25701 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2019-12-18 16:35:04.830 INFO 25701 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 892 ms2019-12-18 16:35:05.045 INFO 25701 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'2019-12-18 16:35:05.254 INFO 25701 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2019-12-18 16:35:05.257 INFO 25701 --- [ main] c.g.HelloWorld.HelloWorldApplication : Started HelloWorldApplication in 1.633 seconds (JVM running for
HelloWorld
创建HelloController
package com.github.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {@GetMapping("hello")public String sayHello() {return "Hello Spring boot\n";}}
@RestController注解相当于@Controller+@ResponseBody组合在一起使用,此注解相当于注解类中方法返回不是视图界面,而是return语句中的内容。
 @GetMapping(“hello”)注解定义了请求路径是/hello
文件结构如下注意点是要将Application类放在最外侧,即包含所有子包 ,HelloWorldApplication.java是当前的启动类 ,一般设置在子包的上一层,spring-boot会自动加载启动类所在包下及其子包下的所有组件.
➜ src git:(master) ✗ tree.├── main│ ├── java│ │ └── com│ │ └── github│ │ ├── controller│ │ │ └── HelloController.java│ │ └── HelloWorldApplication.java│ └── resources│ ├── application.properties│ ├── static│ └── templates└── test└── java└── com└── github└── HelloWorld└── HelloWorldApplicationTests.java
测试结果:
✗ curl "http://127.0.0.1:8080/hello"Hello Spring boot
/src/main/java 目录下放置所有Java 文件(源文件)
/src/main/resources 用于存放所有的资源文件 包括静态资源文件,配置文件,等
/src/main/resources/static 存放各类静态资源
/src/main/resources/application.properties 配置文件 支持两种配置文件类型,即.properties和.yml
/src/main/resources/templates 存放模板文件,如Thymeleaf模板 现在都是前后端分离 会越来越少
/src/test/java 单元测试类Java代码
/target 放置编译后的.class 文件,配置文件
热更新部署
打开Preference->Build,Execution,Deployment->Compiler->勾选Build project automatically ,如果嫌麻烦直接打开idea项目配置选项:输入 Compiler , 并且勾选上 Build project automatically
选择Help->Find Action 或者mac os快捷键是command + shift + A
输入Registry选择第一个
找到“complier.automake.allow.when.app.running”->勾选此项
工程目录结构
工程代码
1.启动类(Application.java)推荐放在根目录com.springboot包下
2.实体类(domain)
   A: com.springboot.domain(jpa项目)
   B: com.springboot.pojo(mybatis项目)
3.数据接口访问层(Dao)
   A: com.springboot.repository(jpa项目)
   B: com.springboot.mapper(mybatis项目)
4.数据服务接口层(Service)推荐:com.springboot.service
5.数据服务实现层(Service Implements)推荐:com.springboot.service.impl
6.前端控制器层(Controller)推荐:com.springboot.controller
7.工具类库(utils)推荐:com.springboot.utils
8.配置类(config)推荐:com.springboot.config
9.数据传输对象(dto)推荐:com.springboot.dto 数据传输对象(Data Transfer Object)用于封装多个实体类(domain)之间的关系,不破坏原有的实体类结构
10.视图包装对象(vo)推荐:com.springboot.vo 视图包装对象(View Object)用于封装客户端请求的数据,防止部分数据泄露(如:管理员ID),保证数据安全,不破坏   原有的实体类结构。
11常量接口类(constant)置于com.springboot.constant
资源配置
根目录:src/main/resources
1.项目配置文件:resources/application.yml
2.静态资源目录:resources/static/ 用于存放html、css、js、图片等资源
3.视图模板目录:resources/templates/ 用于存放jsp、thymeleaf等模板文件
4.mybatis映射文件:resources/mapper/(mybatis项目)
5.mybatis配置文件:resources/mapper/config/(mybatis项目)
