第一节:初始化
一、使用IDEA初始化Spring项目
1、在idea的plugs中搜索Spring,并安装spring-assistant
2、file-new-project,然后选择Spring-Assistant,根据引导选择我们需要使用的Spring工具包
3、一般勾选的工具:
- DeveloperTools : Spring Boot DevTools
- Spring Boot DevTools :由Spring提供的开发工具,提供热重载,重加载等方便的功能
- Lombok: 使用这个的话IDEA需要安装Lombok的相关插件,否则使用起来会很不方便,Lombok提供了许多方便的注解,用于entity,javaBean的使用,比如@Data:自动为所有的属性字段生成Getter、Setter等
- Web:Spring-Web
- Security:Spring-Security
- SQL:数据库使用
- MYSQL Driver
- Spring Data JPA
最终可以生成一个项目包,打开后引入IDEA,让idea引导索引项目
二、使用IDEA打开项目,打开后idea会自动发现maven项目,这时我们可以用maven来编译并启动项目
双击spring-boot:start了
第二节 快速体验Spring MVC
一、修改Springboot的启动类,暂时关闭JPA和Security,因为本节我们还没进入数据库的使用(注意exclude)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
//暂时关闭Security 和 JPA
@SpringBootApplication(exclude= {
DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class
})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
二、创建TestController
三、@RestController,标记为Rest接口类的Controller,这样的Controller不需要View
@RequestMapping("/test") 表示这个Controller下的所有接口都是以 /test 开头
@GetMapping("/helloworld") 这是个快捷注解,表示以GET请求 /test/helloworld 可以访问这个方法
package com.example.demo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/helloworld")
public String helloworld(){
return "HelloWorld";
}
}
三、启动项目,双击spring-boot:start的maven插件,启动项目,通过浏览器访问http://localhost:8080/test/helloworld
此时可以看到HelloWorld
第三节、SpringBoot部分配置选项(简化版)
1、简单的列出一点选项,下回再补充一篇完整的
server.port:配置启动时的端口
server.context-path: 访问路径的context,配置后在路径上需要增加
server.session-timeout=30 session的过期时间
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8 编码
2、修改启动banner
- 在src/main/sources下新建一个banner.txt
- 到http://patorjk.com/software/taag 生成我们要图案
- 复制图案到banner.txt中
- 启动项目就能看到效果